结果集没有hasNext方法。我想检查resultSet是否有任何值
这条路对吗
if (!resultSet.next() ) {
System.out.println("no data");
}
结果集没有hasNext方法。我想检查resultSet是否有任何值
这条路对吗
if (!resultSet.next() ) {
System.out.println("no data");
}
当前回答
你可以这样做
boolean found = false;
while ( resultSet.next() )
{
found = true;
resultSet.getString("column_name");
}
if (!found)
System.out.println("No Data");
其他回答
为什么不使用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)”似乎工作得很好。
我认为检查结果集最简单的方法是通过包org.apache.commons.collections.CollectionUtils下的CollectionUtils
if(CollectionUtils.isNotEmpty(resultList)){
/**
* do some stuff
*/
}
这将检查null以及空结果集条件。
有关更详细的信息,您可以参考下面的文档。 CollectionUtils
ResultSet result = stmt.executeQuery(sqlQuery);
if (!result.next())
status = "ERROR";
else
status = "SUCCESS";
我创建了以下方法来检查ResultSet是否为空。
public static boolean resultSetIsEmpty(ResultSet rs){
try {
// We point the last row
rs.last();
int rsRows=rs.getRow(); // get last row number
if (rsRows == 0) {
return true;
}
// It is necessary to back to top the pointer, so we can see all rows in our ResultSet object.
rs.beforeFirst();
return false;
}catch(SQLException ex){
return true;
}
}
有以下几点考虑是非常重要的:
CallableStatement对象必须设置为让to ResultSet对象走在末尾并返回到顶部。
TYPE_SCROLL_SENSITIVE: ResultSet对象可以移到末尾并返回顶部。进一步可以捕捉最后的变化。
CONCUR_READ_ONLY:可以读取ResultSet对象数据,但不能更新。
CallableStatement proc = dbconex.prepareCall(select, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
if (resultSet==null ) {
System.out.println("no data");
}