问题描述
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 如何检查是否有结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!