Java tutorial
/* * This file is part of AceQL. * AceQL: Remote JDBC access over HTTP. * Copyright (C) 2015, KawanSoft SAS * (http://www.kawansoft.com). All rights reserved. * * AceQL is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * AceQL is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301 USA * * Any modifications to this file must keep this entire header * intact. */ package org.kawanfw.test.api.client; import java.sql.Connection; import java.sql.DatabaseMetaData; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.RowId; import java.sql.SQLException; import org.apache.commons.lang3.SystemUtils; import org.junit.Assert; import org.junit.Test; import org.kawanfw.commons.util.Base64; import org.kawanfw.sql.api.client.RemoteConnection; import org.kawanfw.sql.api.util.SqlUtil; import org.kawanfw.test.parms.ConnectionLoader; import org.kawanfw.test.parms.SqlTestParms; import org.kawanfw.test.util.MessageDisplayer; public class RowIdTest { @Test public void test() throws Exception { Connection connection = null; try { connection = ConnectionLoader.getAceqlConnection(); test(connection); } finally { if (connection != null) { connection.close(); } } } public static void main(String[] args) throws Exception { if (SystemUtils.IS_JAVA_1_7) { System.setProperty("java.net.preferIPv4Stack", "true"); } SqlTestParms.REMOTE = true; new RowIdTest().test(); } /** * @param connection * the AceQL Connection * * @throws SQLException * @throws Exception */ public void test(Connection connection) throws SQLException, Exception { MessageDisplayer.initClassDisplay(this.getClass().getSimpleName()); DatabaseMetaData databaseMetaData = connection.getMetaData(); MessageDisplayer.display( "databaseMetaData.getDatabaseProductName() : " + databaseMetaData.getDatabaseProductName()); if (!new SqlUtil(connection).isOracle()) { MessageDisplayer.display("RowId tests are only supported in Oracle"); return; } String sql = "select customer_id, rowid from customer where customer_id = ?"; PreparedStatement prepStatement = connection.prepareStatement(sql); prepStatement.setInt(1, 1); ResultSet rs = prepStatement.executeQuery(); RowId rowIdCustomer1 = null; while (rs.next()) { int customerId = rs.getInt("customer_id"); rowIdCustomer1 = rs.getRowId(2); byte[] bytes = rowIdCustomer1.getBytes(); String base64 = Base64.byteArrayToBase64(bytes); MessageDisplayer.display("customerId : " + customerId); MessageDisplayer.display("rowId : " + rowIdCustomer1.toString()); MessageDisplayer.display("rowId.hashCode(): " + rowIdCustomer1.hashCode()); MessageDisplayer.display("rowId.getBytes(): " + base64); } rs.close(); boolean doUpdate = true; if (connection instanceof RemoteConnection) { RemoteConnection connectionHttp = (RemoteConnection) connection; if (connectionHttp.isStatelessMode()) { MessageDisplayer.display("setRowId is not supported in stateless mode"); doUpdate = false; } } if (doUpdate) { sql = "update customer set lname = ? where rowid = ?"; PreparedStatement prepStatement2 = connection.prepareStatement(sql); prepStatement2.setString(1, "ROWID"); prepStatement2.setRowId(2, rowIdCustomer1); int rc = prepStatement2.executeUpdate(); MessageDisplayer.display(""); MessageDisplayer.display("rc: " + rc); prepStatement2.close(); } sql = "select customer_id, rowid from customer where customer_id = ?"; prepStatement = connection.prepareStatement(sql); prepStatement.setInt(1, 1); ResultSet rs2 = prepStatement.executeQuery(); RowId rowIdCustomer1New = null; while (rs2.next()) { int customerId = rs2.getInt("customer_id"); rowIdCustomer1New = rs2.getRowId(2); byte[] bytes = rowIdCustomer1New.getBytes(); String base64 = Base64.byteArrayToBase64(bytes); MessageDisplayer.display("customerId : " + customerId); MessageDisplayer.display("rowId : " + rowIdCustomer1New.toString()); MessageDisplayer.display("rowId.hashCode(): " + rowIdCustomer1New.hashCode()); MessageDisplayer.display("rowId.getBytes(): " + base64); } MessageDisplayer.display(""); MessageDisplayer.display("Test RowIds are equal:"); Assert.assertEquals("Set/read RowIds are equal", true, rowIdCustomer1.equals(rowIdCustomer1New)); MessageDisplayer.display("Done!"); prepStatement.close(); rs2.close(); } }