Example usage for java.sql RowId getBytes

List of usage examples for java.sql RowId getBytes

Introduction

In this page you can find the example usage for java.sql RowId getBytes.

Prototype

byte[] getBytes();

Source Link

Document

Returns an array of bytes representing the value of the SQL ROWID designated by this java.sql.RowId object.

Usage

From source file:org.kawanfw.sql.servlet.sql.ResultSetWriter.java

/**
 * Format the column as a RowId//from  www  .j  a  va  2s .c o m
 * 
 * @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.test.api.client.RowIdTest.java

/**
 * @param connection// ww w  .jav  a2  s.  c  om
 *            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();

}