Example usage for java.sql PreparedStatement setRowId

List of usage examples for java.sql PreparedStatement setRowId

Introduction

In this page you can find the example usage for java.sql PreparedStatement setRowId.

Prototype

void setRowId(int parameterIndex, RowId x) throws SQLException;

Source Link

Document

Sets the designated parameter to the given java.sql.RowId object.

Usage

From source file:org.kawanfw.test.api.client.RowIdTest.java

/**
 * @param connection// www  . j  a va  2 s  .c o 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();

}