Java ResultSet如何检查是否有任何结果 [英] Java ResultSet how to check if there are any results

查看:159
本文介绍了Java ResultSet如何检查是否有任何结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

结果集没有方法for hasNext。我想检查resultSet是否有任何值

Resultset has no method for hasNext. I want to check if the resultSet has any value

这是正确的方法

if (!resultSet.next() ) {
    System.out.println("no data");
} 


推荐答案

这是正确的,最初是 ResultSet 的光标指向第一行之前,如果第一次调用 next()返回 false 然后 ResultSet 中没有数据。

That's correct, initially the ResultSet's cursor is pointing to before the first row, if the first call to next() returns false then there was no data in the ResultSet.

如果你使用这个方法,您可能必须在重置之后立即调用 beforeFirst(),因为它现在已将自己定位在第一行之后。

If you use this method, you may have to call beforeFirst() immediately after to reset it, since it has positioned itself past the first row now.

但是应该注意,下面的 Seifer的回答是这个问题的更优雅的解决方案。

It should be noted however, that Seifer's answer below is a more elegant solution to this question.

这篇关于Java ResultSet如何检查是否有任何结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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