DB2JdbcRowSet

The DB2JdbcRowSet is a connected RowSet, meaning that it can only be used with the support of an underlying Connection object, PreparedStatement object, or ResultSet object. Its implementation adheres closely to the description of a JdbcRowSet.

Use DB2JdbcRowSet

Because the DB2JdbcRowSet object supports events described in the Java™ Database Connectivity (JDBC) 3.0 specification for all RowSets, it can serve as an intermediate object between a local database and other objects that must be notified about changes to the database data.

As an example, assume that you are working in an environment where you have a main database and several Personal Digital Assistants (PDAs) that use a wireless protocol to connect to it. A DB2JdbcRowSet object can be used to move to a row and update it by using a master application that is running on the server. The row update causes an event to be generated by the RowSet component. If there is a service running that is responsible for sending out updates to the PDAs, it can register itself as a "listener" of the RowSet. Each time that it receives a RowSet event, it can generate the appropriate update and send it out to the wireless devices.

Refer to Example: DB2JdbcRowSet events for more information.

Create JDBCRowSets

There are several methods provided for creating a DB2JDBCRowSet object. Each is outlined as follows.

Use DB2JdbcRowSet properties and DataSources

DB2JdbcRowSets have properties that accept an SQL query and a DataSource name. The DB2JdbcRowSets are then ready to be used. The following is an example of this approach. The reference to the DataSource named BaseDataSource is assumed to be a valid DataSource that has been previously set up.

Example: Use DB2JdbcRowSet properties and DataSources

Note: Read the Code example disclaimer for important legal information.

    // Create a new DB2JdbcRowSet
    DB2JdbcRowSet jrs = new DB2JdbcRowSet();
 
    // Set the properties that are needed for 
    // the RowSet to be processed.
    jrs.setDataSourceName("BaseDataSource");
    jrs.setCommand("select col1 from cujosql.test_table");
 
    // Call the RowSet execute method. This method causes 
    // the RowSet to use the DataSource and SQL query 
    // specified to prepare itself for data processing.
    jrs.execute();
 
    // Loop through the data in the RowSet.
    while (jrs.next()) {
        System.out.println("v1 is " + jrs.getString(1));
    } 
    
    // Eventually, close the RowSet.
    jrs.close();
Use DB2JdbcRowSet properties and JDBC URLs

DB2JdbcRowSets have properties that accept an SQL query and a JDBC URL. The DB2JdbcRowSets are then ready to be used. The following is an example of this approach:

Example: Use DB2JdbcRowSet properties and JDBC URLs

Note: Read the Code example disclaimer for important legal information.
    // Create a new DB2JdbcRowSet
    DB2JdbcRowSet jrs = new DB2JdbcRowSet();
 
    // Set the properties that are needed for 
    // the RowSet to be processed.
    jrs.setUrl("jdbc:db2:*local");
    jrs.setCommand("select col1 from cujosql.test_table");
 
    // Call the RowSet execute method. This causes 
    // the RowSet to use the URL and SQL query specified
    // previously to prepare itself for data processing.
    jrs.execute();
 
    // Loop through the data in the RowSet.
    while (jrs.next()) {
        System.out.println("v1 is " + jrs.getString(1));
    } 
    
    // Eventually, close the RowSet.
    jrs.close();
Use the setConnection(Connection) method to use an existing database connection

To promote the reuse of JDBC Connection objects, the DB2JdbcRowSet allows you to pass an established connection to the DB2JdbcRowSet. This connection is used by the DB2JdbcRowSet to prepare itself for usage when the execute method is called.

Example: Use the setConnection method

Note: Read the Code example disclaimer for important legal information.
    // Establish a JDBC Connection to the database.
    Connection conn = DriverManager.getConnection("jdbc:db2:*local");
    
    // Create a new DB2JdbcRowSet.
    DB2JdbcRowSet jrs = new DB2JdbcRowSet();
 
    // Set the properties that are needed for 
    // the RowSet to use an established connection.
    jrs.setConnection(conn);
    jrs.setCommand("select col1 from cujosql.test_table");
 
    // Call the RowSet execute method. This causes 
    // the RowSet to use the connection that it was provided
    // previously to prepare itself for data processing.
    jrs.execute();
 
    // Loop through the data in the RowSet.
    while (jrs.next()) {
        System.out.println("v1 is " + jrs.getString(1));
    } 
    
    // Eventually, close the RowSet.
    jrs.close();

Access data and cursor movement

Manipulation of the cursor position and access to the database data through a DB2JdbcRowSet are handled by the underlying ResultSet object. Tasks that can be done with a ResultSet object also apply to the DB2JdbcRowSet object.

Change data and reflecting changes to the underlying database

Support for updating the database through a DB2JdbcRowSet is handled completely by the underlying ResultSet object. Tasks that can be done with a ResultSet object also apply to the DB2JdbcRowSet object.

Related concepts
RowSet characteristics
DB2CachedRowSet