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

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

问题描述

Resultset 没有方法对于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天全站免登陆