结果集没有hasNext方法。我想检查resultSet是否有任何值

这条路对吗

if (!resultSet.next() ) {
    System.out.println("no data");
} 

当前回答

你总是可以在前面做下一个,只做一个后循环检查

if (!resultSet.next() ) {
    System.out.println("no data");
} else {

    do {
     //statement(s)
    } while (resultSet.next());
}

其他回答

ResultSet result = stmt.executeQuery(sqlQuery);
if (!result.next())
    status = "ERROR";
else
    status = "SUCCESS";

假设您正在使用一个新返回的ResultSet,其游标指向第一行之前,一个更简单的检查方法是调用isBeforeFirst()。这避免了在读取数据时必须回溯。

正如文档中解释的那样,如果游标不在第一条记录之前,或者结果集中没有行,则返回false。

if (!resultSet.isBeforeFirst() ) {    
    System.out.println("No data"); 
} 

 

最好使用ResultSet.next()和do{…} while()语法。

“检查任何结果”调用ResultSet.next()将光标移动到第一行,因此使用do{…} while()语法来处理该行,同时继续处理循环返回的其余行。

通过这种方式,您可以检查任何结果,同时也可以处理返回的任何结果。

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());
}

通过使用resultSet.next(),您可以轻松地获得结果,无论resultSet是否包含任何值

ResultSet resultSet = preparedStatement.executeQuery();
if(resultSet.next())
 //resultSet contain some values
else
 // empty resultSet

我相信这是一篇实用且易于阅读的文章。

        if (res.next()) {
            do {

                // successfully in. do the right things.

            } while (res.next());
        } else {
           // no results back. warn the user.
        }