Java ResultSet 如何检查是否有结果
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/867194/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java ResultSet how to check if there are any results
提问by kal
采纳答案by ninesided
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
.
没错,最初ResultSet
的游标指向第一行之前,如果第一次调用next()
返回,false
则ResultSet
.
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.
如果您使用此方法,您可能必须beforeFirst()
在重置它之后立即调用,因为它现在已将自己定位到第一行之后。
It should be noted however, that Seifer's answerbelow is a more elegant solution to this question.
然而,应该注意的是,下面赛弗的回答是这个问题的更优雅的解决方案。
回答by kgiannakakis
You would usually do something like this:
你通常会做这样的事情:
while ( resultSet.next() ) {
// Read the next item
resultSet.getString("columnName");
}
If you want to report an empty set, add a variable counting the items read. If you only need to read a single item, then your code is adequate.
如果要报告空集,请添加一个变量来计算读取的项目。如果您只需要阅读单个项目,那么您的代码就足够了。
回答by Nuoji
That would work if you want to see if there are any rows in the result set yes.
如果您想查看结果集中是否有任何行,那将起作用。
Note that next()
always moves to the next row, so if you are planning on doing any reading from the result set you need to take that into account.
请注意,next()
总是移动到下一行,因此如果您打算从结果集中读取任何内容,则需要考虑到这一点。
Usual usage with ResultSet (when simply reading) is:
ResultSet 的通常用法(仅读取时)是:
while (resultSet.next())
{
... read from the row here ...
}
Which obviously won't work correctly if you invoked next()
once already to check if the result set was empty, so watch out for that. Although there are methods for "backing up", they are not supported for all types of result sets.
如果您next()
已经调用一次以检查结果集是否为空,这显然无法正常工作,因此请注意这一点。尽管有“备份”方法,但并非所有类型的结果集都支持它们。
回答by Maslow
you could always do the next up front, and just do a post loop check
你总是可以预先做下一个,然后做一个循环后检查
if (!resultSet.next() ) {
System.out.println("no data");
} else {
do {
//statement(s)
} while (resultSet.next());
}
回答by Seifer
Assuming you are working with a newly returned ResultSet
whose cursor is pointing before the first row, an easier way to check this is to just call isBeforeFirst()
. This avoids having to back-track if the data is to be read.
假设您正在处理一个新返回的,ResultSet
其光标指向第一行之前,一个更简单的检查方法是调用isBeforeFirst()
. 如果要读取数据,这避免了必须回溯。
As explained in the documentation, this returns false if the cursor is not before the first record or if there are no rows in the ResultSet.
如文档中所述,如果游标不在第一条记录之前或者ResultSet 中没有行,则返回 false 。
if (!resultSet.isBeforeFirst() ) {
System.out.println("No data");
}
回答by user868927
if(resultSet.first) {
} else {
system.out.println("No raw or resultSet is empty");
}
Because if ResultSet has no raw then resultSet.first
returns false.
因为如果 ResultSet 没有 raw 则resultSet.first
返回 false。
回答by Dermot Doherty
Best to use ResultSet.next() along with the do {...} while() syntax for this.
最好将 ResultSet.next() 与 do {...} while() 语法一起使用。
The "check for any results" call ResultSet.next() moves the cursor to the first row, so use the do {...} while() syntax to process that row while continuing to process remaining rows returned by the loop.
“检查任何结果”调用 ResultSet.next() 将光标移动到第一行,因此使用 do {...} while() 语法处理该行,同时继续处理循环返回的剩余行。
This way you get to check for any results, while at the same time also processing any results returned.
通过这种方式,您可以检查任何结果,同时还处理返回的任何结果。
if(resultSet.next()) { // Checks for any results and moves cursor to first row,
do { // Use 'do...while' to process the first row, while continuing to process remaining rows
} while (resultSet.next());
}
回答by Deepu Surendran
ResultSet result = stmt.executeQuery(sqlQuery);
if (!result.next())
status = "ERROR";
else
status = "SUCCESS";
回答by Felype
To be totally sure of rather the resultset is empty or not regardless of cursor position, I would do something like this:
为了完全确定结果集是否为空,无论光标位置如何,我都会这样做:
public static boolean isMyResultSetEmpty(ResultSet rs) throws SQLException {
return (!rs.isBeforeFirst() && rs.getRow() == 0);
}
This function will return true if ResultSet is empty, false if not or throw an SQLException if that ResultSet is closed/uninitialized.
如果 ResultSet 为空,则此函数将返回 true,否则返回 false;如果该 ResultSet 已关闭/未初始化,则抛出 SQLException。
回答by Anthony Oyovwe
The best thing for to do is to check the first row so that when you intend to get the data you can avoid the mistake of skipping a row. Something like: if (!resultSet.first() ) { System.out.println("no data"); }
最好的做法是检查第一行,这样当您打算获取数据时,可以避免跳过一行的错误。类似于: if (!resultSet.first() ) { System.out.println("no data"); }