O'Reilly logo

Java Enterprise in a Nutshell, Third Edition by William Crawford, Jim Farley

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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();

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required