问题描述
没有方法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.
但是应该注意,下面的是这个问题的更优雅的解决方案。
It should be noted however, that Seifer's answer below is a more elegant solution to this question.
这篇关于Java ResultSet如何检查是否有任何结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!