Java JDBC 延迟加载的结果集 [英] Java JDBC Lazy-Loaded ResultSet

查看:29
本文介绍了Java JDBC 延迟加载的结果集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让您通过运行 JDBC 查询获得的 ResultSet 延迟加载?我希望每一行在我请求时加载,而不是事先加载.

Is there a way to get a ResultSet you obtain from running a JDBC query to be lazily-loaded? I want each row to be loaded as I request it and not beforehand.

推荐答案

简答:

在调用executeQuery()之前使用Statement.setFetchSize(1).

这在很大程度上取决于您使用的 JDBC 驱动程序.你可能想看看 这个页面,描述 MySQL、Oracle、SQL Server 和 DB2 的行为.

This depends very much on which JDBC driver you are using. You might want to take a look at this page, which describes the behavior of MySQL, Oracle, SQL Server, and DB2.

主要收获:

  • 每个数据库(即每个 JDBC 驱动程序)都有自己的默认行为.
  • 有些驱动程序会尊重 setFetchSize() 而没有任何警告,而其他驱动程序则需要一些帮助".
  • Each database (i.e. each JDBC driver) has its own default behavior.
  • Some drivers will respect setFetchSize() without any caveats, whereas others require some "help".

MySQL 是一个特别奇怪的案例.请参阅这篇文章.听起来如果你调用setFetchSize(Integer.MIN_VALUE),那么它会一次下载一行,但不是很清楚.

MySQL is an especially strange case. See this article. It sounds like if you call setFetchSize(Integer.MIN_VALUE), then it will download the rows one at a time, but it's not perfectly clear.

另一个例子:这里是文档PostgreSQL 行为.如果自动提交打开,那么 ResultSet 将一次获取所有行,但如果关闭,则您可以按预期使用 setFetchSize().

Another example: here's the documentation for the PostgreSQL behavior. If auto-commit is turned on, then the ResultSet will fetch all the rows at once, but if it's off, then you can use setFetchSize() as expected.

最后一件事要记住:这些 JDBC 驱动程序设置只影响客户端发生的事情.服务器可能仍会将整个结果集加载到内存中,但您可以控制客户端下载结果的方式.

One last thing to keep in mind: these JDBC driver settings only affect what happens on the client side. The server may still load the entire result set into memory, but you can control how the client downloads the results.

这篇关于Java JDBC 延迟加载的结果集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆