7

I would like to write a java function that takes in a SQL query and returns a ResultSet for processing elsewhere. This can't be done as a ResultSet is dead once the connection is closed.

Googling around I found a VERY OLD (2004) OReilly article that had something that looked like the cure: CachedRowSet. You just drop in your ResultSet, the CachedRowSet saves the data, lets you close the connection and play with the data elsewhere using the returned CachedRowSet.

The article references implementations of the CachedRowSet by Sun, which appear to be nowhere to be found.

Modern javadocs ( for Java 1.5 and up ) seem to have something by the same name, "CachedRowSet", that is more than just a holder of ResultSet data. That "CachedRowSet" seems to do the entire database processing from getting connections and everything else.

Is THAT "CachedRowSet" the same thing as is talked about in the old article?

I would like something simple, like in the old article. Something to plop a ResultSet into for processing after the conneciton is closed.

Is there such an animal?

Thanks

Steve
  • 3,127
  • 14
  • 56
  • 96

3 Answers3

5

CachedRowSet is a standard Java interface. Sun wrote a reference implementation, and the Sun/Oracle JDK contains it. If you're using a different JDK, that or another implementation may or may not be available.

If you already have a ResultSet, then you can fill a CachedRowSet from it using the populate method.

If you are forward-thinking enough to be using Java 7, then you can obtain a CachedRowSet instance in a portable way, using a RowSetFactory, which has a createCachedRowSet method. You can get a RowSetFactory from a RowSetProvider (of course!).

Tom Anderson
  • 46,189
  • 17
  • 92
  • 133
-1

javax.sql.rowset.CachedRowSet is merely an interface. There's a Sun/Oracle proprietary implementation, but it's unsupported and thus risky to use.

Most code these days follows the "convert to pojos" model.

bluish
  • 26,356
  • 27
  • 122
  • 180
MeBigFatGuy
  • 28,272
  • 7
  • 61
  • 66
  • MBFG; I'm using Oracle and I was hoping to use this as a temporary measure while I clean up some legacy code and learn a better way to do things datawise ( database frameworks, etc ). I wasn't able to find anything about where to get it, how to use it. Any clues beyond more Googling? – Steve Apr 27 '12 at 00:01
  • How would using pojos replace getting a "ResultSet" to scroll through outside of the function where it was obtained? – Steve Apr 27 '12 at 00:02
  • 1
    What makes you think `com.sun.rowset.CachedRowSetImpl` is unsupported? The [CachedRowSet interface](http://docs.oracle.com/javase/7/docs/api/javax/sql/rowset/CachedRowSet.html) is in Java 7, and its javadoc states that "the reference implementation of the CachedRowSet interface provided by Oracle Corporation is a standard implementation. Developers may use this implementation just as it is". The reference implementation is `com.sun.rowset.CachedRowSetImpl`; this does not sound unsupported to me. – Tom Anderson Jun 22 '12 at 16:27
  • @tom But if you try to compile using that implementation class, the following warning appears: "CachedRowSetImpl is internal proprietary API and may be removed in a future release". I think the correct approach is the one you outlined above, using the RowSet's factory/provider. – PaoloC Apr 14 '14 at 09:00
  • @PaoloC: Using the provider and factory definitely seems preferable. I suspect the warning you get is a blanket warning your compiler applies to all `com.sun.*` imports. I still think the javadoc implies that it is in fact not an internal proprietary API. But yeah, since everyone's using Java 7+ now, use the provider! – Tom Anderson Apr 14 '14 at 11:12
  • It is not unsupported. If you disagree please provide evidence. *Using the class directly* is unsupported. – user207421 Apr 08 '16 at 02:08
-1

If you just want to transfer the data without the need of all the functions that CachedRowSet gives, you're better off just putting the result set data in an List of Lists.

   List<Object> list = new ArrayList<Object>();
   ResultSet rs = stmt.executeQuery("select * from myTable");
   while(rs.next()) {
         List<Object> row = new ArrayList<Object>();
         row.add(rs.getObject(1));
         row.add(rs.getObject(2));
         row.add(rs.getObject(3));
         //And so on, or you can use the ResultSetMetaData to determine the number of columns
         list.add(row);
   }
   rs.close();

When you are done you can send that list object anywhere you want and then iterate through it to get the data.

george_h
  • 1,562
  • 2
  • 19
  • 37