Section 8.12. RowSets


8.12. RowSets

JDBC 2.0 introduced the RowSet interface. But unlike ResultSet, RowSet encapsulates data using a JavaBean interface, allowing it to participate in the JavaBean event model. A RowSet, as the name implies, encapsulates a set of rows produced by a query. Since a RowSet is a Java bean, it can be used easily in a graphical development environment. In addition to the RowSet interface itself, the JDBC 3.0 API introduced a set of standard interfaces for particular kinds of row sets.

Sun's JDK 1.4 environment doesn't ship with any RowSet implementations. However, J2SE 5.0 does, and for earlier releases, Sun has made a number of implementations available at http://java.sun.com/products/jdbc/download.html. The Sun implementations are found in the com.sun.rowset package and have class names that echo the interface names, followed by Impl. Here's how to use the simplest of these, JdbcRowSet, which simply encapsulates a ResultSet:

  JdbcRowSet jdbcRowSet         = new JdbcRowSetImpl(  );  jdbcRowSet.setCommand("SELECT * FROM CUSTOMERS WHERE CUSTNO = ?");  jdbcRowSet.setUrl("jdbc:oracle:thin:@dbhost.co.com:1521:ORCL");  jdbcRowSet.setUsername("SAMSON");  jdbcRowSet.setPassword("DELILAH");  jdbcRowSet.setType(ResultSet.TYPE_SCROLL_INSENSITIVE);  jdbcRowSet.setConcurrency(ResultSet.CONCUR_UPDATABLE);  jdbcRowSet.setInt(1, 10);  jdbcRowSet.execute(  );    jdbcRowSet.first(  );  System.out.println(jdbcRowSet.getString(1));  jdbcRowSet.last(  );  System.out.println(jdbcRowSet.getString(1));    jdbcRowSet.close(  );

The getXXX and setXXX methods are the same as in the ResultSet interface. Support for scrollable and updateable row sets depends on the underlying driver and database implementation.

The addRowSetListener( ) method of RowSet can be used to register other components as listeners. They must implement the RowSetListener interface to be implemented.

The com.sun.rowset package also contains a CachedRowSet object that will hold a ResultSet independently of the originating JDBC connection:

 Connection con = DriverManager.getConnection(dbURL, dbUser, dbPassword); Statement stmt = con.createStatement(  ); ResultSet rs = stmt.executeQuery("SELECT * FROM CUSTOMERS"); CachedRowSet crs = new CachedRowSetImpl(  ); crs.populate(rs); rs.close(  ); stmt.close(  ); crs.next(  ); System.out.println(crs.getString("CUSTNAME"));

In order to update a CachedRowSet, you must set the JDBC connection information and specify the underlying SQL via the setCommand( ) method. Now, let's continue our code sample:

 crs.setUrl(dbURL); crs.setUsername(dbUser); crs.setPassword(dbPassword); crs.setCommand("SELECT * FROM CUSTOMERS"); //... crs.setString("CUSTNAME", "John Smith"); crs.updateRow(  ); crs.moveToCurrentRow(  ); crs.acceptChanges(  );

Note that we call the acceptChanges( ) method after performing our update. This is necessary to propagate the changes back to the original DataSource.

The final RowSet available from Sun is the WebRowSet, which is identical to the CachedRowSet but has a persistence engine based on XML.

While a RowSet object would generally be used with JDBC, there is no actual requirement that this be so. RowSet implementations could be written to act against tabular data, text files, and more esoteric storage mechanisms.



Java Enterprise in a Nutshell
Java Enterprise in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596101422
EAN: 2147483647
Year: 2004
Pages: 269

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net