[英]CachedRowSet: can it still be used to hold ResultSet data?
I would like to write a java function that takes in a SQL query and returns a ResultSet for processing elsewhere. 我想编写一个java函数,它接受一个SQL查询并返回一个ResultSet以便在别处进行处理。 This can't be done as a ResultSet is dead once the connection is closed.
一旦连接关闭,ResultSet就会失效,因此无法完成此操作。
Googling around I found a VERY OLD (2004) OReilly article that had something that looked like the cure: CachedRowSet. 谷歌搜索我发现了一篇非常古老的(2004)OReilly文章 ,看起来像是治愈了: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.
您只需放入ResultSet,CachedRowSet保存数据,让您关闭连接并使用返回的CachedRowSet在其他地方播放数据。
The article references implementations of the CachedRowSet by Sun, which appear to be nowhere to be found. 本文引用了Sun的CachedRowSet实现,似乎无处可寻。
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. 现代javadocs(适用于Java 1.5及更高版本)似乎具有相同名称的“CachedRowSet”,它不仅仅是ResultSet数据的持有者。 That "CachedRowSet" seems to do the entire database processing from getting connections and everything else.
“CachedRowSet”似乎通过获取连接和其他所有内容来完成整个数据库处理。
Is THAT "CachedRowSet" the same thing as is talked about in the old article? 那个“CachedRowSet”和旧文章中谈到的一样吗?
I would like something simple, like in the old article. 我想要一些简单的东西,比如旧文章。 Something to plop a ResultSet into for processing after the conneciton is closed.
在连接关闭后将ResultSet放入进行处理的东西。
Is there such an animal? 有这样的动物吗?
Thanks 谢谢
CachedRowSet is a standard Java interface. CachedRowSet是标准的Java接口。 Sun wrote a reference implementation, and the Sun/Oracle JDK contains it .
Sun编写了一个参考实现, Sun / Oracle JDK包含它 。 If you're using a different JDK, that or another implementation may or may not be available.
如果您使用的是其他JDK,那么该实现可能会也可能不会实现。
If you already have a ResultSet
, then you can fill a CachedRowSet
from it using the populate
method. 如果您已有
ResultSet
,则可以使用populate
方法从中填充CachedRowSet
。
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. 如果您正在考虑使用Java 7,那么您可以使用
RowSetFactory
以可移植的方式获取CachedRowSet
实例,该RowSetFactory
具有createCachedRowSet
方法。 You can get a RowSetFactory
from a RowSetProvider
(of course!). 你可以得到一个
RowSetFactory
从RowSetProvider
(当然!)。
javax.sql.rowset.CachedRowSet
is merely an interface. javax.sql.rowset.CachedRowSet
只是一个接口。 There's a Sun/Oracle proprietary implementation, but it's unsupported and thus risky to use. 有一个Sun / Oracle专有实现,但它不受支持,因此使用起来很危险。
Most code these days follows the "convert to pojos" model. 如今大多数代码遵循“转换为pojos”模型。
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. 如果您只想传输数据而不需要CachedRowSet提供的所有函数,那么最好只将结果集数据放入列表列表中。
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. 完成后,您可以将列表对象发送到任何您想要的位置,然后迭代它以获取数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.