List of usage examples for java.sql RowId hashCode
int hashCode();
RowId
object. From source file:org.kawanfw.sql.servlet.sql.ResultSetWriter.java
/** * Format the column as a RowId/*from ww w . j a v a 2 s.c om*/ * * @param resultSet * @param columnIndex * @return * @throws SQLException * @throws IOException */ private String formatRowIdColumn(ResultSet resultSet, int columnIndex) throws SQLException, IOException { RowId rowId = resultSet.getRowId(columnIndex); if (!connectionId.equals("0")) { ConnectionStore connectionStore = new ConnectionStore(username, connectionId); Connection connection = connectionStore.get(); if (connection == null) { throw new SQLException(SqlReturnCode.SESSION_INVALIDATED); } connectionStore.put(rowId); } RowIdHttp rowIdHttp = new RowIdHttp(rowId.hashCode(), rowId.getBytes()); RowIdTransporter rowIdTransporter = new RowIdTransporter(); String base64 = rowIdTransporter.toBase64(rowIdHttp); return base64; }
From source file:org.kawanfw.sql.servlet.sql.ServerPreparedStatementParameters.java
/** * Set the Prepared Statement with the passed parameters Parameters are * ordered and in lists [type, value] in the List * // ww w . java2 s . c o m */ public void setParameters() throws SQLException, IOException { Map<Integer, Integer> parameterTypes = statementHolder.getParameterTypes(); Map<Integer, String> parameterStringValues = statementHolder.getParameterStringValues(); for (int i = 0; i < parameterTypes.size(); i++) { int parameterIndex = i + 1; Integer paramTypeInteger = parameterTypes.get(parameterIndex); String paramValue = parameterStringValues.get(parameterIndex); // paramValue = HtmlConverter.fromHtml(paramValue); if (paramTypeInteger == null) { throw new IllegalArgumentException(Tag.PRODUCT_PRODUCT_FAIL + "parameterType in List {parameterType, parameterValue, } is null!"); } int paramType = paramTypeInteger.intValue(); debug("index : " + parameterIndex + ":"); debug("paramType : " + paramType + ":"); debug("paramValue: " + paramValue + ":"); // parameterValues.add(paramValue); if (paramType == JavaTypes.ASCII_STREAM) { setAsciiStream(preparedStatement, parameterIndex, paramValue); parameterValues.put(i, paramValue); } else if (paramType == JavaTypes.BIG_DECIMAL) { preparedStatement.setBigDecimal(parameterIndex, new BigDecimal(paramValue)); parameterValues.put(i, new BigDecimal(paramValue)); } else if (paramType == JavaTypes.BOOLEAN) { preparedStatement.setBoolean(parameterIndex, Boolean.parseBoolean(paramValue)); parameterValues.put(i, Boolean.parseBoolean(paramValue)); } else if (paramType == JavaTypes.DATE) { java.sql.Date date = extractDate(paramValue); preparedStatement.setDate(parameterIndex, date); parameterValues.put(i, date); } else if (paramType == JavaTypes.DOUBLE) { preparedStatement.setDouble(parameterIndex, Double.parseDouble(paramValue)); parameterValues.put(i, Double.parseDouble(paramValue)); } else if (paramType == JavaTypes.FLOAT) { preparedStatement.setFloat(parameterIndex, Float.parseFloat(paramValue)); parameterValues.put(i, Float.parseFloat(paramValue)); } else if (paramType == JavaTypes.INPUT_STREAM) { debug("Before setBinaryStream"); setBinaryStream(preparedStatement, parameterIndex, paramValue); parameterValues.put(i, paramValue); debug("After setBinaryStream"); } else if (paramType == JavaTypes.INT) { preparedStatement.setInt(parameterIndex, Integer.parseInt(paramValue)); parameterValues.put(i, Integer.parseInt(paramValue)); } else if (paramType == JavaTypes.LONG) { preparedStatement.setLong(parameterIndex, Long.parseLong(paramValue)); parameterValues.put(i, Long.parseLong(paramValue)); } else if (paramType == JavaTypes.READER) { setCharacterStream(preparedStatement, parameterIndex, paramValue); parameterValues.put(i, paramValue); } else if (paramType == JavaTypes.SHORT) { preparedStatement.setShort(parameterIndex, Short.parseShort(paramValue)); parameterValues.put(i, Short.parseShort(paramValue)); } else if (paramType == JavaTypes.STRING) { if (paramValue.startsWith(TransportConverter.KAWANFW_BYTES)) { byte[] bytes = TransportConverter.fromTransportFormatToBytes(paramValue); debug("paramValue.startsWith(TransportConverter.KAWANFW_BYTES): " + bytes); preparedStatement.setBytes(parameterIndex, bytes); parameterValues.put(i, bytes); } else { paramValue = HtmlConverter.fromHtml(paramValue); preparedStatement.setString(parameterIndex, extractString(paramValue)); parameterValues.put(i, extractString(paramValue)); } } else if (paramType == JavaTypes.NSTRING) { paramValue = HtmlConverter.fromHtml(paramValue); preparedStatement.setNString(parameterIndex, extractString(paramValue)); parameterValues.put(i, extractString(paramValue)); } else if (paramType == JavaTypes.TIME) { Time time = extractTime(paramValue); preparedStatement.setTime(parameterIndex, time); parameterValues.put(i, time); } else if (paramType == JavaTypes.TIMESTAMP) { Timestamp ts = extractTimestamp(paramValue); preparedStatement.setTimestamp(parameterIndex, ts); parameterValues.put(i, ts); } else if (paramType == JavaTypes.URL) { debug("URL paramValue = " + paramValue); UrlTransporter urlTransporter = new UrlTransporter(); URL url = null; try { url = urlTransporter.fromBase64(paramValue); preparedStatement.setURL(parameterIndex, url); parameterValues.put(i, url); } catch (ClassNotFoundException e) { String reason = Tag.PRODUCT_EXCEPTION_RAISED + " Impossible to convert BASE64 serialized URL back to URL"; throw new SQLException(reason, e); } } else if (paramType == JavaTypes.ARRAY) { String connectionId = request.getParameter(ConnectionParms.CONNECTION_ID); ConnectionStore connectionStore = new ConnectionStore(username, connectionId); Connection connection = connectionStore.get(); if (connection == null) { throw new SQLException(SqlReturnCode.SESSION_INVALIDATED); } debug("ARRAY paramValue = " + paramValue); paramValue = HtmlConverter.fromHtml(paramValue); // Get back the array referenced by this hash code: Array array = connectionStore.getArray(Integer.parseInt(paramValue)); preparedStatement.setArray(parameterIndex, array); parameterValues.put(i, array); } else if (paramType == JavaTypes.ROWID) { String connectionId = request.getParameter(ConnectionParms.CONNECTION_ID); ConnectionStore connectionStore = new ConnectionStore(username, connectionId); Connection connection = connectionStore.get(); if (connection == null) { throw new SQLException(SqlReturnCode.SESSION_INVALIDATED); } debug("RowId paramValue = " + paramValue); RowIdTransporter rowIdTransporter = new RowIdTransporter(); RowId rowIdHttp = null; try { rowIdHttp = rowIdTransporter.fromBase64(paramValue); // Get back the rowId referenced by this hash code: RowId rowId = connectionStore.getRowId(rowIdHttp.hashCode()); preparedStatement.setRowId(parameterIndex, rowId); parameterValues.put(i, rowId); } catch (ClassNotFoundException e) { String reason = Tag.PRODUCT_EXCEPTION_RAISED + " Impossible to convert BASE64 serialized RowId back to RowId"; throw new SQLException(reason, e); } } else if (paramType == JavaTypes.NULL) { int javaType = Integer.parseInt(paramValue); preparedStatement.setNull(parameterIndex, javaType); parameterValues.put(i, null); } else { // Includes the null value management if (paramValue == null || paramValue.equals("null")) { paramValue = null; } preparedStatement.setObject(parameterIndex, paramValue); parameterValues.put(i, (Object) paramValue); } } }
From source file:org.kawanfw.test.api.client.RowIdTest.java
/** * @param connection//from w w w . j ava 2s . co m * 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(); }