结果集没有hasNext方法。我想检查resultSet是否有任何值
这条路对吗
if (!resultSet.next() ) {
System.out.println("no data");
}
结果集没有hasNext方法。我想检查resultSet是否有任何值
这条路对吗
if (!resultSet.next() ) {
System.out.println("no data");
}
当前回答
如果您想查看结果集中是否有任何行,这将起作用。
请注意,next()总是移动到下一行,因此如果您计划从结果集中进行任何读取操作,则需要考虑到这一点。
通常使用ResultSet(当简单读取时)是:
while (resultSet.next())
{
... read from the row here ...
}
如果已经调用next()一次来检查结果集是否为空,那么显然不能正确工作,所以要注意这一点。尽管存在用于“备份”的方法,但并非所有类型的结果集都支持这些方法。
其他回答
if (resultSet==null ) {
System.out.println("no data");
}
我相信这是一篇实用且易于阅读的文章。
if (res.next()) {
do {
// successfully in. do the right things.
} while (res.next());
} else {
// no results back. warn the user.
}
你总是可以在前面做下一个,只做一个后循环检查
if (!resultSet.next() ) {
System.out.println("no data");
} else {
do {
//statement(s)
} while (resultSet.next());
}
为什么不使用rs.getRow()?
int getRow()
throws SQLException
Retrieves the current row number. The first row is number 1, the second number 2, and so on.
Note:Support for the getRow method is optional for ResultSets with a result set type of TYPE_FORWARD_ONLY
Returns:
the current row number; 0 if there is no current row
Throws:
SQLException - if a database access error occurs or this method is called on a closed result set
SQLFeatureNotSupportedException - if the JDBC driver does not support this method
Since:
1.2
对我来说,检查“if (rs.getRow() != 0)”似乎工作得很好。
你可以这样做
boolean found = false;
while ( resultSet.next() )
{
found = true;
resultSet.getString("column_name");
}
if (!found)
System.out.println("No Data");