There are several ways to place data into a DB2CachedRowSet.
DB2CachedRowSets have a populate method that can be used to put data into the RowSet from a DB2ResultSet object. The following is an example of this approach.
Example: Use the populate method
// Establish a connection to the database. Connection conn = DriverManager.getConnection("jdbc:db2:*local"); // Create a statement and use it to perform a query. Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("select col1 from cujosql.test_table"); // Create and populate a DB2CachedRowSet from it. DB2CachedRowSet crs = new DB2CachedRowSet(); crs.populate(rs); // Note: Disconnect the ResultSet, Statement, // and Connection used to create the RowSet. rs.close(); stmt.close(); conn.close(); // Loop through the data in the RowSet. while (crs.next()) { System.out.println("v1 is " + crs.getString(1)); } crs.close();
DB2CachedRowSets have properties that allow the DB2CachedRowSets to accept an SQL query and a DataSource name. They then use the SQL query and DataSource name to create data for themselves. 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 DB2CachedRowSet properties and DataSources
// Create a new DB2CachedRowSet DB2CachedRowSet crs = new DB2CachedRowSet(); // Set the properties that are needed for // the RowSet to use a DataSource to populate itself. crs.setDataSourceName("BaseDataSource"); crs.setCommand("select col1 from cujosql.test_table"); // Call the RowSet execute method. This causes // the RowSet to use the DataSource and SQL query // specified to populate itself with data. Once // the RowSet is populated, it disconnects from the database. crs.execute(); // Loop through the data in the RowSet. while (crs.next()) { System.out.println("v1 is " + crs.getString(1)); } // Eventually, close the RowSet. crs.close();
DB2CachedRowSets have properties that allow the DB2CachedRowSets to accept an SQL query and a JDBC URL. They then use the query and JDBC URL to create data for themselves. The following is an example of this approach.
Example: Use DB2CachedRowSet properties and JDBC URLs
// Create a new DB2CachedRowSet DB2CachedRowSet crs = new DB2CachedRowSet(); // Set the properties that are needed for // the RowSet to use a JDBC URL to populate itself. crs.setUrl("jdbc:db2:*local"); crs.setCommand("select col1 from cujosql.test_table"); // Call the RowSet execute method. This causes // the RowSet to use the DataSource and SQL query // specified to populate itself with data. Once // the RowSet is populated, it disconnects from the database. crs.execute(); // Loop through the data in the RowSet. while (crs.next()) { System.out.println("v1 is " + crs.getString(1)); } // Eventually, close the RowSet. crs.close();
To promote the reuse of JDBC Connection objects, the DB2CachedRowSet provides a mechanism for passing an established Connection object to the DB2CachedRowSet that is used to populate the RowSet. If a user-supplied Connection object is passed in, the DB2CachedRowSet does not disconnect it after populating itself.
Example: Use setConnection(Connection) method to use an existing database connection
// Establish a JDBC connection to the database. Connection conn = DriverManager.getConnection("jdbc:db2:*local"); // Create a new DB2CachedRowSet DB2CachedRowSet crs = new DB2CachedRowSet(); // Set the properties that are needed for the // RowSet to use an already connected connection // to populate itself. crs.setConnection(conn); crs.setCommand("select col1 from cujosql.test_table"); // Call the RowSet execute method. This causes // the RowSet to use the connection that it was provided // with previously. Once the RowSet is populated, it does not // close the user-supplied connection. crs.execute(); // Loop through the data in the RowSet. while (crs.next()) { System.out.println("v1 is " + crs.getString(1)); } // Eventually, close the RowSet. crs.close();
To promote the reuse of JDBC Connection objects, the DB2CachedRowSet provides a mechanism for passing an established Connection object to the DB2CachedRowSet when the execute method is called. If a user-supplied Connection object is passed in, the DB2CachedRowSet does not disconnect it after populating itself.
Example: Use execute(Connection) method to use an existing database connection
// Establish a JDBC connection to the database. Connection conn = DriverManager.getConnection("jdbc:db2:*local"); // Create a new DB2CachedRowSet DB2CachedRowSet crs = new DB2CachedRowSet(); // Set the SQL statement that is to be used to // populate the RowSet. crs.setCommand("select col1 from cujosql.test_table"); // Call the RowSet execute method, passing in the connection // that should be used. Once the Rowset is populated, it does not // close the user-supplied connection. crs.execute(conn); // Loop through the data in the RowSet. while (crs.next()) { System.out.println("v1 is " + crs.getString(1)); } // Eventually, close the RowSet. crs.close();
To reduce the database's workload, the DB2CachedRowSet provides a mechanism for grouping SQL statements for several threads into one processing request for the database.
Example: Use execute(int) method to group database requests
// Create a new DB2CachedRowSet DB2CachedRowSet crs = new DB2CachedRowSet(); // Set the properties that are needed for // the RowSet to use a DataSource to populate itself. crs.setDataSourceName("BaseDataSource"); crs.setCommand("select col1 from cujosql.test_table"); // Call the RowSet execute method. This causes // the RowSet to use the DataSource and SQL query // specified to populate itself with data. Once // the RowSet is populated, it disconnects from the database. // This version of the execute method accepts the number of seconds // that it is willing to wait for its results. By // allowing a delay, the RowSet can group the requests // of several users and only process the request against // the underlying database once. crs.execute(5); // Loop through the data in the RowSet. while (crs.next()) { System.out.println("v1 is " + crs.getString(1)); } // Eventually, close the RowSet. crs.close();