The RowSet Interface

A RowSet is a lightweight ResultSet object designed as a Javabeans component. Data within the row set can be retrieved and modified using accessor/mutator methods. The RowSet is defined as an extension to the normal java.sql packages. That is why you will not find RowSet in the java.sql package but you will find it in the javax.sql package. Because RowSets are modeled as Java bean components, event notifications are available for the row sets. The RowSet interface extends the ResultSet interface and consequently inherits all the methods of the ResultSet interface.

RowSets, similar to ResultSets, can be used to access tabular data from a database. In addition, RowSets can be used to access data from a non-database source like a spreadsheet or a structured file. RowSets have the same capabilities as a ResultSet; that is, they use methods to retrieve the data. They also feature the scrollable and updatable features of the ResultSet.

Use RowSets in instances where the JDBC driver does not support scrollable and updatable result sets.

A RowSet can be used either in a connected or disconnected mode. In a connected mode, the row set is connected to the database and the database connection is alive. In the disconnected mode, the row set is not connected to the database and there is no connection available to the database. Because there is no database connection open for a rowset in a disconnected mode, a limited subset of the JDBC API is available to retrieve and manipulate the data. RowSets in disconnected mode are especially useful because they can be serialized along with their data and metadata and then sent across a network. Serialization with result sets is not possible because a result set has to have a database connection open.

Three types of RowSets are available:

  • CachedRowSet A disconnected row set that stores the data in memory. This row set should be used for manipulating small amounts of data.

  • JDBCRowSet A connected row set that acts as a wrapper on the JDBC driver's result set. The JDBCRowSet acts as a Javabeans component and has accessor/mutator methods that retrieve and update the data in the rowset.

  • WebRowSet A connected row set that interacts with a data access provider application like a Java servlet using the HTTP protocol. This type of row set is primarily available for use by thin client Web-enabled applications.

Take a look at a code snippet using the CachedRowSet:

 // create a statement object  Statement myStmt = myCon.createStatement(); // execute the query and retrieve the result set ResultSet myRSet = myStmt.executeQuery("SELECT BOOK_NAME FROM BOOK_TBL"); // create a cached rowset object CachedRowSet myCRowSet = new CachedRowSet(); // associate the result set to the cached row set myCRowSet.populate(myRset); // now use the cached row set instead of the result set as any // java data type to retrieve the data while(myCRowSet.next) {     System.out.println("Book Name = " + myCRowSet.getString(1) );     ... } 

Use the existing methods of the ResultSet interface to retrieve the data from the cached RowSet.

Conversely, you can use the CachedRowSet interface directly to execute the SQL statement and retrieve the data. The code snippet for this technique of using the CachedRowSet interface follows:

 // create a cached rowset object  CachedRowSet myCRowSet = new CachedRowSet(); // build the SQL statement in the cached rowset object myCRowSet.setCommand("SELECT BOOK_NAME FROM BOOK_TBL"); // execute the query. The result set data is directly // populated within the CachedRowSet object myCRowSet.execute(); // now use the cached row set instead of the result set as any // java data type to retrieve the data while(myCRowSet.next) {     System.out.println("Book Name = " + myCRowSet.getString(1) );     ... } 

RowSetMetaData

RowSetMetaData is very similar to ResultSetMetaData. A RowSetMetaData object is associated with a RowSet. The RowSetMetaData provides methods to obtain extra information about the data in the RowSet. RowSet metadata assumes significance in disconnected RowSets where a live database connection is not present. Any program using the RowSet relies on the RowSetMetaData object to provide correct information to retrieve data.



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net