Skip navigation.
Home

Get disconnected with CachedRowSet

In JDK 1.4, when a you wanted to retrive data from a database into a ResultSet, you should always leave the Connection opend till you mainpluate the data in the ResultSet, if you closed the Connection, data in the ResultSet are gone.
So, in 1.4 the following code is not okay:



Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url, name, pass);
Statement stmt = c.createStatement();
ResultSet rs = s.executeQuery ( query );
con.close() // Closing connection b4 using rs is not okay
while (rs.next()) {
     //.....
}


Java 5 solution


Java 5 has come with soem new exciting features, among these feature is the new JDBC APIs: CachedRowSet


A CachedRowSet object is a container for rows of data that caches its rows in memory, which makes it possible to operate without always being connected to its data source. Further, it is a JavaBeansTM component and is scrollable, updatable, and serializable. A CachedRowSet object typically contains rows from a result set, but it can also contain rows from any file with a tabular format, such as a spread sheet. The reference implementation supports getting data only from a ResultSet object, but developers can extend the SyncProvider implementations to provide access to other tabular data sources.

An application can modify the data in a CachedRowSet object, and those modifications can then be propagated back to the source of the data.


At this release, as an aid to developers, the RowSet interface has been implemented (as JSR 114) in five of the more common ways a RowSet object can be used. These implementations provide a standard that developers are free to use as is or to extend.

Following are the five standard implementations:


  • JdbcRowSet - used to encapsulate a result set or a driver that is implemented to use JDBC technology

  • CachedRowSet - disconnects from its data source and operates independently except when it is getting data from the data source or writing modified data back to the data source. This makes it a lightweight container
    for as much data as it can store in memory.

  • FilteredRowSet - extends CachedRowSet and is used to get a subset of data
  • JoinRowSet - extends CachedRowSet and is used to get an SQL JOIN of data from multiple RowSet objects
  • WebRowSet - extends CachedRowSet and is used for XML data. It describes tabular components in XML using a standardized XML schema.  

Read More here:
http://java.sun.com/developer/technicalArticles/javaserverpages/cachedrowset/

I am a little bit confused!

I am a little bit confused! Is it about the JDBC version (1.x , 2, 3 or 4) or the JDKs ?! \\ Plus the article is dated February 2001 where 1.5 was not there, was it?

Re: I am a littel bit confused!

I may not illustrated this efficiently, but any way, The CachedRowSet interface is part of the JDBC 2.0 API specification and it is included with the new J2SE release (J2SE 1.5, AKA "Tiger"). It implements both javax.sql.RowSet and ResultSet. for tha article date, CachedRowSet may have an intial draft that was submitted to JCP early (perhaps at 2000), and it was approved and included in Java5. ------------- Amr Kourany Software Engineer IBM WTC, Egypt Branchb

Still confused! Because

Still confused! Because what I seem to remember that JDBC 2.0 was included in JDK 1.2, wasn't?! \\

Well, I have checked that

Well, I have checked that out. My claim is correct. I have also searched the [JCP site| http://www.jcp.org ] and found that the first JDBC version to go for JCP was actually [JDBC 3.0 | http://www.jcp.org/en/jsr/detail?id=54] not JDBC 2.0 which was included in JDK 1.2 long time ago!

Here are my sources

Well, I guiss it is better that you check the sources which calims that CachedRowSet is part of JDBC 2.0 here is one http://www.onjava.com/pub/a/onjava/2004/06/23/cachedrowset.html and check these too http://www.theserverside.com/news/thread.tss?thread_id=24969 http://www.jcp.org/en/jsr/detail?id=114 I would like to distinguish tow things, Rowset Interface, and CachedRowSet Implementation. What was added in JDK1.5 is just a new implementation to RowSet, but the original RowSet Interface is in JDBC since long time ago. ------------- Amr Kourany Software Engineer IBM WTC, Egypt Branchb