What Is a RowSet Object? in JDBC

The javax.sql package provides the rowset API, which makes it easy to handle data sets from virtually any data source as JavaBeans components. A RowSet object represents a set of rows returned by a database server as a result of running a SQL query or opening a table. According to JDBC API Tutorial and Reference, Third Edition (Addison-Wesley Professional, 2003), “a RowSet object is a container for tabular data, encapsulating a set of zero or more rows that have been retrieved from a data source. In a basic implementation of the RowSet interface, the rows are retrieved from a JDBC data source, but a ‘row set’ may be customized so that its data can also be from a spreadsheet, a flat file, or any other data source with a tabular format. A RowSet object extends the ResultSet interface, which means that it can be scrollable, can be updatable, and can do anything a ResultSet object can do.”

Different kinds of rowsets (these are all interfaces and extend the RowSet) are provided by J2SE 5.0 (I will provide details on these different rowsets in the following questions and answers):

Cached rowset:(javax.sql.CachedRowSet): A CachedRowSet object is special in that it can operate without being connected to its data source—that is, it is a disconnected RowSet object (disconnected rowsets are those that do not maintain a connection with their data sources).

JDBC rowset:(javax.sql.JdbcRowSet): A wrapper around a ResultSet object that makes it possible to use the result set as a JavaBeans component. Thus, a JdbcRowSet object can be one of the beans that a tool makes available for composing an application. A JdbcRowSet is a connected rowset.

Web rowset:(javax.sql.WebRowSet): A WebRowSet object is very special because in addition to offering all of the capabilities of a CachedRowSet object, it can write itself as an XML document and can also read that XML document to convert itself back to a WebRowSet object.

Filtered rowset:(javax.sql.FilteredRowSet): There are occasions when a RowSet object has a need to provide a degree of filtering to its contents. One possible solution is to provide a query language for all standard RowSet implementations; however, this is not a practical approach for lightweight components such as disconnected RowSet objects. The FilteredRowSet interface seeks to address this need without supplying a heavyweight query language along with the processing that such a query language would require.

Join rowset:(javax.sql.JoinRowSet): The JoinRowSet interface provides a mechanism for combining related data from different RowSet objects into one JoinRowSet object, which represents a SQL JOIN. In other words, a JoinRowSet object acts as a container for the data from RowSet objects that form a SQL JOIN relationship.

Synchronized Resolver rowset:(javax.sql.rowset.spi.SyncResolver): A SyncResolver object is a specialized RowSet object that implements the SyncResolver interface. It may operate as either a connected RowSet object(an implementation of the JdbcRowSet interface) or a connected RowSet object(an implementation of the CachedRowSet interface or one of its subinterfaces).

The following is taken from the J2SE 5.0 documentation:

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.

And the following is taken from JDBC API Tutorial and Reference:

Rowsets may be either connected or disconnected.A connected RowSet object maintains a connection to its data source the entire time it is in use, whereas a disconnected rowset is connected to its data source only while it is reading data from the data source or writing data to it. While the rowset is disconnected, it does not need a JDBC driver or the full implementation of the JDBC API. This makes it very lean and therefore an ideal container for sending a set of data to a thin client. The client can, if it chooses, make updates to the data and send the rowset back to the application server. On the server, the disconnected RowSet object uses its reader to make a connection to the data source and write data back to it. Exactly how this is done depends on how the reader is implemented. Typically, the reader delegates making a connection and reading/writing data to the JDBC driver.

A rowset may be disconnected from a data source, that is, function without maintaining an open connection to a data source the whole time it is in use. In addition, a rowset can be serialized, which means that it can be sent to a remote object over a network (e.g., RMI Server).

According to the J2SE 5.0 documentation, the RowSet interface works with various other classes and interfaces behind the scenes. These can be grouped into three categories:

  • Event notification
    • RowSetListener: A RowSet object is a JavaBeanscomponent because it has properties and participates in the JavaBeans event notification mechanism. The RowSetListener interface is implemented by a component that wants to be notified about events that occur to a particular RowSet object. Such a component registers itself as a listener with a rowset via the RowSet.addRowSetListener() method. When the RowSet object changes one or all of its rows, or moves its cursor, it also notifies each listener that is registered with it. The listener reacts by carrying out its implementation of the notification method called on it.
    • RowSetEvent: As part of its internal notification process, a RowSet object creates an instance of RowSetEvent and passes it to the listener. The listener can use this RowSetEvent object to find out which rowset had the event.
  • Metadata
  • RowSetMetaData: This interface, derived from the ResultSetMetaData interface, provides information about the columns in a RowSet object. An application can use RowSetMetaData methods to find out how many columns the rowset contains and what kind of data each column can contain. The RowSetMetaData interface provides methods for setting the information about columns, but an application would not normally use these methods. When an application calls the RowSet method execute, the RowSet object will contain a new set of rows, and its RowSetMetaData object will have been internally updated to contain information about the new columns.
  • The Reader/Writer facility: A RowSet object that implements the RowSetInternal interface can call on the RowSetReader object associated with it to populate itself with data. It can also call on the RowSetWriter object associated with it to write any changes to its rows back to the data source from which it originally got the rows. A rowset that remains connected to its data source does not need to use a reader and writer because it can simply operate on the data source directly.
  • RowSetInternal: By implementing the RowSetInternal interface, a RowSet object gets access to its internal state and is able to call on its reader and writer. A rowset keeps track of the values in its current rows and of the values that immediately preceded the current ones, referred to as the original values. A rowset also keeps track of(1) the parameters that have been set for its command and(2) the connection that was passed to it, if any. A rowset uses the RowSetInternal methods behind the scenes to get access to this information. An application does not normally invoke these methods directly.
  • RowSetReader: A disconnected RowSet object that has implemented the RowSetInternal interface can call on its reader (the RowSetReader object associated with it) to populate it with data. When an application calls the RowSet.execute() method, that method calls on the rowset’s reader to do much of the work. Implementations can vary widely, but generally a reader makes a connection to the data source, reads data from the data source and populates the rowset with it, and closes the connection. A reader may also update the RowSetMetaData object for its rowset. The rowset’s internal state is also updated, either by the reader or directly by the method RowSet.execute().
  • RowSetWriter: A disconnected RowSet object that has implemented the RowSetInternal interface can call on its writer (the RowSetWriter object associated with it) to write changes back to the underlying data source. Implementations may vary widely, but generally, a writer will(1) make a connection to the data source,(2) check to see whether there is a conflict, that is, whether a value that has been changed in the rowset has also been changed in the data source,(3) write the new values to the data source if there is no conflict, and (4) close the connection.

The RowSet interface may be implemented in any number of ways, and anyone may write an implementation. Developers are encouraged to use their imaginations in coming up with new ways to use rowsets.



All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

JDBC Topics