3.2.  Use the JDBC 4.1 RowSetProvider, RowSetFactory, and new RowSet interfaces

[Note]
  • [ROWSET_TECHNOTE] RowSet 1.1: Creating Row Sets with RowSetFactory Interface and RowSetProvider Class

The javax.sql.RowSet is the interface that adds support to the JDBC API for the JavaBeans component model. A rowset, which can be used as a JavaBeans component in a visual Bean development environment, can be created and configured at design time and executed at run time.

The RowSet interface provides a set of JavaBeans properties that allow a RowSet instance to be configured to connect to a JDBC data source and read some data from the data source. A group of setter methods (setInt(...), setBytes(...), setString(...), and so on) provide a way to pass input parameters to a rowset's command property. This command is the SQL query the rowset uses when it gets its data from a relational database, which is generally the case.

The RowSet interface supports JavaBeans events, allowing other components in an application to be notified when an event occurs on a rowset, such as a change in its value.

The RowSet interface is unique in that it is intended to be implemented using the rest of the JDBC API. In other words, a RowSet implementation is a layer of software that executes "on top" of a JDBC driver. Implementations of the RowSet interface can be provided by anyone, including JDBC driver vendors who want to provide a RowSet implementation as part of their JDBC products.

A RowSet object may make a connection with a data source and maintain that connection throughout its life cycle, in which case it is called a connected rowset. A rowset may also make a connection with a data source, get data from it, and then close the connection. Such a rowset is called a disconnected rowset. A disconnected rowset may make changes to its data while it is disconnected and then send the changes back to the original source of the data, but it must reestablish a connection to do so.

A disconnected rowset may have a reader (a RowSetReader object) and a writer (a RowSetWriter object) associated with it. The reader may be implemented in many different ways to populate a rowset with data, including getting data from a non-relational data source. The writer can also be implemented in many different ways to propagate changes made to the rowset's data back to the underlying data source.

Rowsets are easy to use. The RowSet interface extends the standard java.sql.ResultSet interface. The RowSetMetaData interface extends the java.sql.ResultSetMetaData interface.

The java.sql.ResultSet interface requires a persistent connection with a database to invoke the INSERT, UPDATE, and DELETE row operations on the database table data. The RowSet interface extends the ResultSet interface and is a container for tabular data that may operate without being connected to the data source. Thus, the RowSet interface reduces the overhead of a persistent connection with the database.

In Java 5 SE, five new implementations of RowSetJdbcRowSet, CachedRowSet, WebRowSet, FilteredRowSet, and JoinRowSet — were introduced. The WebRowSet interface extends the RowSet interface and is the XML document representation of a RowSet object. A WebRowSet object represents a set of fetched database table rows, which may be modified without being connected to the database.

You can use an instance of RowSetFactory to create a RowSet object. The following example uses an instance of RowSetFactory to create the JdbcRowSet object, jdbcRs:

public void testJdbcRowSet(String username, String password) throws SQLException {

    RowSetFactory myRowSetFactory = null;
    JdbcRowSet jdbcRs = null;
    ResultSet rs = null;
    Statement stmt = null;

    try {

        myRowSetFactory = RowSetProvider.newFactory();
        jdbcRs = myRowSetFactory.createJdbcRowSet();

        jdbcRs.setUrl("jdbc:myDriver:myAttribute");
        jdbcRs.setUsername(username);
        jdbcRs.setPassword(password);

        jdbcRs.setCommand("SELECT COF_NAME, SUP_ID, PRICE, SALES, TOTAL FROM COFFEES");
        jdbcRs.execute();

        // ...
					

The following statement creates the RowSetProvider object myRowSetFactory with the default RowSetFactory implementation, com.sun.rowset.RowSetFactoryImpl:

RowSetFactory myRowSetFactory = RowSetProvider.newFactory();
CachedRowSet crs = myRowSetFactory.createCachedRowSet()
					

Alternatively, if your JDBC driver has its own RowSetFactory implementation, you can specify it as an argument of newFactory(...) method:

RowSetFactory rsf = RowSetProvider.newFactory("com.sun.rowset.RowSetFactoryImpl", null);
WebRowSet wrs = rsf.createWebRowSet();
					

The following statements create the JdbcRowSet object jdbcRs and configure its database connection properties:

jdbcRs = myRowSetFactory.createJdbcRowSet();

jdbcRs.setUrl("jdbc:myDriver:myAttribute");
jdbcRs.setUsername(username);
jdbcRs.setPassword(password);
					

The RowSetFactory interface contains methods to create the different types of RowSet implementations available in JDBC 4.1 and later:

package javax.sql.rowset;

public interface RowSetFactory {

    CachedRowSet createCachedRowSet();     // Creates a new instance of a CachedRowSet

    FilteredRowSet createFilteredRowSet(); // Creates a new instance of a FilteredRowSet

    JdbcRowSet createJdbcRowSet();         // Creates a new instance of a JdbcRowSet

    JoinRowSet createJoinRowSet();         // Creates a new instance of a JoinRowSet

    WebRowSet createWebRowSet();           // Creates a new instance of a WebRowSet
}
					

Professional hosting         Free 'Oracle Certified Expert Web Services Developer 6' Guide     Free SCDJWS 5.0 Guide