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
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.
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.
When you are done you can send that list object anywhere you want and then iterate through it to get the data.
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 aCachedRowSet
from it using thepopulate
method.If you are forward-thinking enough to be using Java 7, then you can obtain a
CachedRowSet
instance in a portable way, using aRowSetFactory
, which has acreateCachedRowSet
method. You can get aRowSetFactory
from aRowSetProvider
(of course!).