如何获取 java.sql.ResultSet 的大小?
这不是一个非常简单的操作吗? 但是,我发现既没有 size()
也没有 length()
方法。
Shouldn't this be a pretty straightforward operation? However, I see there's neither a size()
nor length()
method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
请执行
SELECT COUNT(*) FROM ...
查询。或者
在任何一种情况下,您都不必循环整个数据。
Do a
SELECT COUNT(*) FROM ...
query instead.OR
In either of the case, you won't have to loop over the entire data.
好吧,如果您有一个
ResultSet.TYPE_FORWARD_ONLY
类型的ResultSet
,您希望保持这种方式(并且不切换到ResultSet.TYPE_SCROLL_INSENSITIVE
或ResultSet.TYPE_SCROLL_INSENSITIVE
以便能够使用.last()
)。我建议一个非常好的和有效的技巧,您可以在顶部添加第一个包含行数的假/假行。
示例
假设您的查询如下,
您的输出如下所示
只需将代码重构为如下所示:
您的查询输出现在将类似于
所以您只需
Well, if you have a
ResultSet
of typeResultSet.TYPE_FORWARD_ONLY
you want to keep it that way (and not to switch to aResultSet.TYPE_SCROLL_INSENSITIVE
orResultSet.TYPE_SCROLL_INSENSITIVE
in order to be able to use.last()
).I suggest a very nice and efficient hack, where you add a first bogus/phony row at the top containing the number of rows.
Example
Let's say your query is the following
and your output looks like
Simply refactor your code to something like this:
Your query output will now be something like
So you just have to
我在使用 rs.last() 时遇到异常
:
这是因为默认情况下它是 ResultSet.TYPE_FORWARD_ONLY ,这意味着您只能使用 rs.next( )
解决方案是:
I got an exception when using
rs.last()
:
it's due to by default it is
ResultSet.TYPE_FORWARD_ONLY
, which means you can only users.next()
the solution is:
[速度考虑]
这里很多人建议
ResultSet.last()
但为此,您需要以ResultSet.TYPE_SCROLL_INSENSITIVE
的形式打开连接,对于 Derby 嵌入式数据库来说,最多可达比ResultSet.TYPE_FORWARD_ONLY
慢 10 倍。根据我对嵌入式 Derby 和 H2 数据库的微观测试,在 SELECT 之前调用
SELECT COUNT(*)
速度要快得多。这是我的代码和基准的更详细信息
[Speed consideration]
Lot of ppl here suggests
ResultSet.last()
but for that you would need to open connection as aResultSet.TYPE_SCROLL_INSENSITIVE
which for Derby embedded database is up to 10 times SLOWER thanResultSet.TYPE_FORWARD_ONLY
.According to my micro-tests for embedded Derby and H2 databases it is significantly faster to call
SELECT COUNT(*)
before your SELECT.Here is in more detail my code and my benchmarks
获取 ResultSet 大小的方法,不需要使用 ArrayList 等
现在你将获得大小,如果你想打印 ResultSet,在打印之前也使用以下代码行,
The way of getting size of ResultSet, No need of using ArrayList etc
Now You will get size, And if you want print the ResultSet, before printing use following line of code too,
这是进行行计数的简单方法。
It is a simple way to do rows-count.
我检查了ResultSet接口的运行时值,发现它几乎一直是一个ResultSetImpl。 ResultSetImpl 有一个名为
getUpdateCount()
的方法,它返回您正在查找的值。这个代码示例应该足够了:
ResultSet resultSet =executeQuery(sqlQuery);
double rowCount = ((ResultSetImpl)resultSet).getUpdateCount()
我意识到向下转型通常是一个不安全的过程,但这个方法还没有让我失望。
I checked the runtime value of the ResultSet interface and found out it was pretty much a ResultSetImpl all the time. ResultSetImpl has a method called
getUpdateCount()
which returns the value you are looking for.This code sample should suffice:
ResultSet resultSet = executeQuery(sqlQuery);
double rowCount = ((ResultSetImpl)resultSet).getUpdateCount()
I realize that downcasting is generally an unsafe procedure but this method hasn't yet failed me.
今天我用这个逻辑为什么我不知道得到RS的计数。
Today, I used this logic why I don't know getting the count of RS.
我也遇到了同样的问题。 在执行后以这种方式使用 ResultSet.first() 解决了这个问题:
文档(链接):
I was having the same problem. Using
ResultSet.first()
in this way just after the execution solved it:Documentation (link):
最简单的方法是运行 Count(*) 查询,执行 resultSet.next() 指向第一行,然后执行 resultSet.getString(1) 来获取计数。 代码 :
Easiest approach, Run Count(*) query, do resultSet.next() to point to the first row and then just do resultSet.getString(1) to get the count. Code :
我在这里看到的评论有点过于手动,所以我碰巧遇到了一个更简单的答案。 希望这可以帮助。
它返回执行查询后获得的所有数据的 int 值。
The comments I can see here are somewhat too manual and I so happen to come across a simpler answer. Hope this helps.
It returns an int of all the data you got after executing the query.
给列一个名称。
将该列从 ResultSet 对象引用到一个 int 中,并从那里执行您的逻辑。
Give column a name..
Reference that column from the ResultSet object into an int and do your logic from there..