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();
Get Java Enterprise in a Nutshell, Third Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.