如何确定获取的Java结果集是否为空?

如何确定获取的Java结果集是否为空?,java,database,jdbc,resultset,Java,Database,Jdbc,Resultset,为什么会这样?您也可以这样做: Class.forName("org.sqlite.JDBC"); Connection conn = DriverManager.getConnection("jdbc:sqlite:userdata.db"); Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery("SELECT * from table WHERE is_query_processed =

为什么会这样?

您也可以这样做:

Class.forName("org.sqlite.JDBC");
Connection conn =
    DriverManager.getConnection("jdbc:sqlite:userdata.db");
Statement stat = conn.createStatement();

ResultSet rs = stat.executeQuery("SELECT * from table WHERE is_query_processed = 0;");

int rowcount = rs.getRow(); 
System.out.println("Row count = "+rowcount); // output 1

rs.first(); // This statement generates an exception

我通常使用的模式如下:

rs.last();
int numberOfRows = rs.getRow();
if(numberOfRows) {
    rs.beforeFirst();
    while(rs.next()) {
        ...
    }
}
List<User> users = userDAO.list();

if (users.isEmpty()) {
    // It is empty!
if (users.size() == 1) {
    // It has only one row!
} else {
    // It has more than one row!
}

用于迭代结果集。因此,如果结果集为空,则results.next()将返回false。

下面是一个简单的方法:

 while (results.next())
警告 这会将光标移到开头。但是如果你只是想测试它是否是空的,你可能还没有对它做任何事情

或者 在进行任何处理之前,请立即使用
first()
方法。 ResultSet rs=stat.executeQuery(“从查询处理的表中选择*)

工具书类

为什么执行不进入程序 while循环


如果您的结果集为空,
rs.next()
方法返回false,并且while循环的主体不会被输入,无论返回的行数(不是计数)
rs.getRow()。Colins示例很有效。

来回移动光标以确定行数不是正常的JDBC实践。正常的JDBC实践是将
ResultSet
映射到值对象的
List
,每个值对象表示一个表行实体,然后使用
List
方法确定是否有行

例如:

if(rs.first()) {
    // there's stuff to do
} else {
    // rs was empty
}

有关其他JDBC示例,请参见。

在提交时关闭游标

public List<User> list() throws SQLException {
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;
    List<User> users = new ArrayList<User>();

    try {
        connection = database.getConnection();
        statement = connection.createStatement();
        resultSet = statement.executeQuery(SQL_LIST);
        while (resultSet.next()) {
            User user = new User();
            user.setId(resultSet.getLong("id"));
            user.setName(resultSet.getString("name"));
            // ...
            users.add(user);
        }
    } finally {
        if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
        if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
        if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
    }

    return users;
}
公共静态final int CLOSE\u游标\u AT\u COMMIT

public List<User> list() throws SQLException {
    Connection connection = null;
    Statement statement = null;
    ResultSet resultSet = null;
    List<User> users = new ArrayList<User>();

    try {
        connection = database.getConnection();
        statement = connection.createStatement();
        resultSet = statement.executeQuery(SQL_LIST);
        while (resultSet.next()) {
            User user = new User();
            user.setId(resultSet.getLong("id"));
            user.setName(resultSet.getString("name"));
            // ...
            users.add(user);
        }
    } finally {
        if (resultSet != null) try { resultSet.close(); } catch (SQLException logOrIgnore) {}
        if (statement != null) try { statement.close(); } catch (SQLException logOrIgnore) {}
        if (connection != null) try { connection.close(); } catch (SQLException logOrIgnore) {}
    }

    return users;
}
试试这个:

The constant indicating that ResultSet objects should be closed when the method Connection.commit is called. 

这样,您就可以正常工作。

也许您可以将结果集对象转换为字符串对象,并检查它是否为空

ResultSet MyResult = null;
MyResult = Conexion.createStatement().executeQuery("Your Query  Here!!!");
MyResult.last();
int NumResut = MyResult.getRow();MyResult.beforeFirst();
//Follow with your other operations....

这将在不跳过第一条记录时检查它是否为空

`if(resultset.toString().isEmpty()){
     // containg null result
 }
 else{
     //This conains the result you want
 }`

但是我的SQL查询返回1行。我已经直接在db上运行了相同的查询。rs.next()之前没有调用任何其他函数,对我有效(仅包括rs中的1行)另请参见第5.1.4节。我刚刚注意到,您说在调用
first()时引发了异常。引发了哪个异常?它可能会抛出一个,因为您已经使用了
getRow()
,也可能会抛出一个,因为您的驱动程序不支持此方法,在这种情况下,Colin的解决方案更可能适合您。@jasonmp:您是对的,我的驱动程序不支持调用first()。我使用了Collin的解决方案,它对我有效。这将向右移动光标?因此,对于进一步的处理,第一条记录可能会丢失。如果您在此代码之后处理记录,则会丢失第一条记录。但是,我通常在同一while循环中处理记录。(其中“//ResultSet processing here comment”为。)该操作按预期工作,并包括所有行。缺点是你不能仅仅将检查粘贴到一个方法中。list方法来自哪个类?它不在结果集上
`if(resultset.toString().isEmpty()){
     // containg null result
 }
 else{
     //This conains the result you want
 }`
if (rs.first()) {
    do {
        // ResultSet is not empty, Iterate over it
    } while (rs.next());
} else {
    // ResultSet is empty
}