6.3.9 Query Data from the Student Table Using the Java RowSet Object
A RowSet object is one of the JavaBeans components with multiple support from JavaBeans, and it is a new feature in the java.sql package. By using the RowSet object, a database query can be performed automatically with the data source connection and query statement creation.
In this section, we will show readers how to use this new feature to reduce the code load and improve the efficiency of the data query with the help of this RowSet object.
6.3.9.1 Introduction to Java RowSet Object
The JDBC 4.0 API includes many new features in the java.sql package as well as the new Standard Extension package, javax.sql. This new JDBC API moves Java applications into the world of heavy-duty database computing. One of the important features is the RowSet object.
A RowSet object contains a set of rows from a result set or some other source of tabular data, like a file or spreadsheet. Because a RowSet object follows the JavaBeans model for properties and event notification, it is a JavaBeans component that can be combined with other components in an application. As it is compatible with other Beans, application developers can use a development tool to create a RowSet object and set its properties.
RowSets may have many different implementations to fill different needs. These implementa-tions fall into two broad categories, connected and disconnected:
1) A connected RowSet is equivalent to a ResultSet, and it maintains a connection to a data source as long as the RowSet is in use.
2) A disconnected RowSet works as a DataSet, and it can connect to a data source to perform data updates periodically. Most of the time, it is disconnected from the data source and uses a mapping memory space as a mapped database.
While a RowSet is disconnected, it does not need a JDBC driver or the full JDBC API, so its foot-print is very small. Thus, a RowSet is an ideal format for sending data over a network to a thin client.
Because it is not continually connected to its data source, a disconnected RowSet stores its data in memory. It needs to maintain metadata about the columns it contains and information about its internal state. It also needs a facility for making connections, for executing commands and for reading and writing data to and from the data source. A connected RowSet, by contrast, opens a connection and keeps it open for as long as the RowSet is being used.
To make writing an implementation easier, the Java Software division of Oracle, Inc., plans to provide reference implementations for five different styles of RowSets. The following list of planned implementations gives you an idea of some of the possibilities.
1) A CachedRowSet class—a disconnected RowSet that caches its data in memory; not suiT-able for very large data sets but an ideal way to provide thin Java clients, such as a personal digital assistant or network computer, with tabular data.
2) A JDBCRowSet class—a connected RowSet that serves mainly as a thin wrapper around a ResultSet object to make a JDBC driver look like a JavaBeans component.
3) A WebRowSet class—a connected RowSet that uses the HTTP protocol internally to talk to a Java Servlet that provides data access; used to make it possible for thin web clients to retrieve and possibly update a set of rows.
4) A FilteredRowSet is an extension to WebRowSet that provides programmatic support for filtering its content. This enables you to avoid the overhead of supplying a query and the processing involved. The SQL implementation of FilteredRowSet is javax.sql. rowset.FilteredRowSet. The Oracle implementation of FilteredRowSet is ora-cle.jdbc.rowset.OracleFilteredRowSet. The OracleFilteredRowSet class in the ojdbc14.jar file implements the standard JSR-114 interface javax.sql.rowset. FilteredRowSet.
5) A JoinRowSet is another extension to WebRowSet that consists of related data from dif-ferent RowSets. There is no standard way to establish a SQL JOIN between disconnected RowSets without connecting to the data source. A JoinRowSet addresses this issue. The SQL implementation of JoinRowSet is the javax.sql.rowset.JoinRowSet class. The Oracle implementation of JoinRowSet is the oracle.jdbc.rowset. OracleJoinRowSet class. This class, which is in the ojdbc14.jar file, implements the standard JSR-114 interface javax.sql.rowset.JoinRowSet. Any number of RowSet objects, which implement the Joinable interface, can be added to a JoinRowSet object, provided they can be related in a SQL JOIN. All five types of RowSet support the Joinable interface. The Joinable interface provides methods for specifying the columns based on which the JOIN will be performed, that is, the match columns.
Next, let’s have a closer look at the operational sequence for the RowSet object.