CachedRowSet: can it be used to store ResultSet data?

I would like to write a java function that takes an SQL query and returns a ResultSet for processing elsewhere. This cannot be done because the ResultSet is dead after the connection is closed.

Google google found a VERY OLD (2004) OReilly article that had something similar to a cure: CachedRowSet. You just drop your ResultSet, CachedRowSet saves the data, allows you to close the connection and play with the data elsewhere using the returned CachedRowSet.

This article mentions CachedRowSet by Sun implementations that seem to be not found anywhere.

Modern javadocs (for Java 1.5 and later) seem to have the same name "CachedRowSet", which is more than just the owner of the ResultSet data. That "CachedRowSet" seems to handle all database processing from receiving connections and everything else.

Is THAT "CachedRowSet" the same as the old article says?

I would like something simple, as in an old article. Something to flip the ResultSet to handle after closing the connection.

Is there such an animal?

thanks

+6
source share
3 answers

CachedRowSet is the standard Java interface. Sun has written a reference implementation, and the Sun / Oracle JDK contains it . If you use another JDK, this or another implementation may or may not be available.

If you already have a ResultSet , you can populate it with a CachedRowSet using the populate method.

If you are pretty good at Java 7, you can get an instance of CachedRowSet portable way using RowSetFactory , which has a createCachedRowSet method. You can get RowSetFactory from RowSetProvider (of course!).

+4
source

javax.sql.rowset.CachedRowSet is just an interface. There is a patented Sun / Oracle implementation, but it is not supported and therefore risky to use.

Most codes these days follow the "convert to pojos" model.

-1
source

If you just want to transfer data without the need for all the functions that CachedRowSet provides, you better just put the resultset data in a 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're done, you can send this list object anywhere and then iterate over it to get the data.

-1
source

Source: https://habr.com/ru/post/914274/


All Articles