如何复制数据库结果集
我想在 JSP 页面中复制从 Oracle 数据库获取的 ResultSet
。我认为简单的重新分配就可以解决问题,但似乎失败了。这是我的代码:
ResultSet rset;
ResultSet new_rset;
rset = alljobsBean.getStatus(conn,1,max ,min );
new_rset = rset;
这是错误的吗?或者是否有一种我不知道的复制 ResultSet
的特殊方法?
I would like to duplicate a ResultSet
that I got from an Oracle database in a JSP page. I thought a simple reassignment would do the trick but it seems to fail. Here is my code:
ResultSet rset;
ResultSet new_rset;
rset = alljobsBean.getStatus(conn,1,max ,min );
new_rset = rset;
Is this wrong? Or is there a special way of duplicating ResultSet
s that I dont know?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除了读取所有数据并将其存储在其他地方之外,没有复制
ResultSet
的标准方法。原因是大多数 ResultSet 实现并不将所有结果存储在其中,而只是保持与数据库的连接,用于根据需要获取结果。There is no standard way to duplicate a
ResultSet
, short of reading all data and storing it somewhere else. The reason for that is that mostResultSet
implementations do not store all results in it but simply keep a connection to the database that is used to get the results as needed.new_rset = rset;
不执行任何复制。它只是将new_rset
指向rset
所指向的同一个对象,而new_rset
和rset
都只是对的引用堆上的同一个对象。如果您需要在 JSP 中复制 ResultSet,请考虑使用 DTO将包含
ResultSet
中的所有数据,因此您无需在视图中保留对ResultSet
的引用。使用 DTO 将使您能够在读取完ResultSet
后关闭连接,同时您可以继续在视图中访问 SQL 查询返回的数据。您还可以考虑使用断开连接的
RowSet
而不是
ResultSet
如果您打算以与ResultSet
类似的方式断开对数据的访问,具有不必保留与数据库的连接。new_rset = rset;
does not perform any duplication. It simply pointsnew_rset
to the same object thatrset
is pointing, with bothnew_rset
andrset
being mere references to the same object on the heap.If you need to duplicate the ResultSet in a JSP, consider using DTOs that will contain all the data within the
ResultSet
so that you need not hold a reference to theResultSet
in the view. Using a DTO will enable you to close the connection once you have finished reading theResultSet
, while you can continue to access the data returned by the SQL query, in a view.You can also consider using a disconnected
RowSet
instead ofResultSet
if you intend to have disconnected access to data, in the similar manner asResultSet
, with the added advantage of not having to retain a connection to the database.如果您希望从数据库获取 ResultSet 并将其显示在 JSP 页面中,您可以考虑使用 CachedRowSet。如果您的目标更加雄心勃勃,您可以考虑 WebRowSet。但对于 80% 的需求,最好将数据从 ResultSet 加载到一些 DTO 中,然后再传递到 JSP。
If you what to get a ResultSet from the database and to show it in a JSP page, you might consider using CachedRowSet. If your goals are more ambitious, you might consider WebRowSet. But for 80% of your needs you would be better of with just loading data from your ResultSet into some DTOs before passing it to JSP.