Java JDBC Lazy-Loaded ResultSet [英] Java JDBC Lazy-Loaded ResultSet

查看:84
本文介绍了Java JDBC Lazy-Loaded ResultSet的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法让你从运行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 Lazy-Loaded ResultSet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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