How to find out if a Java ResultSet obtained is em

2019-04-20 09:40发布

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

Why is it so?

10条回答
贪生不怕死
2楼-- · 2019-04-20 10:23

CLOSE_CURSORS_AT_COMMIT

public static final int CLOSE_CURSORS_AT_COMMIT

The constant indicating that ResultSet objects should be closed when the method Connection.commit is called. 
查看更多
迷人小祖宗
3楼-- · 2019-04-20 10:30

May be you can convert your resultset object into String object and check whether is it empty or not.

`if(resultset.toString().isEmpty()){
     // containg null result
 }
 else{
     //This conains the result you want
 }`
查看更多
聊天终结者
4楼-- · 2019-04-20 10:31

You can do this too:

rs.last();
int numberOfRows = rs.getRow();
if(numberOfRows) {
    rs.beforeFirst();
    while(rs.next()) {
        ...
    }
}
查看更多
兄弟一词,经得起流年.
5楼-- · 2019-04-20 10:33
 while (results.next())

is used to iterate over a result set.so results.next() will return false if its empty.

查看更多
登录 后发表回答