How to check if a Resultset is empty?

Asked

Viewed 694 times

1

I’m making a query in Mysql and when the query exists it returns the good values.

But here’s the question, what’s the value ResultSet receives when the search does not exist? Type look for a name in the table records if that name does not exist in the database which is returned?

I’ve searched the Internet but can’t find anything related.

1 answer

1


You can check whether the method ResultSet.html#isBeforeFirst returns false:

con = DriverManager.getConnection( ... );
stmt = con.prepareStatement( ... );

ResultSet rs = stmt.executeQuery();

if (!rs.isBeforeFirst() ) {    
    // Não há dados, faça algo aqui...
} 

The isBeforeFirst will return false if the cursor not before the first registration or if there are no lines in the ResultSet, or true otherwise.

  • Thank you very much! worked perfectly! I was wondering where I can research more on the subject. is there any specific book detailing the JDBC?

  • @Christianooliveira Dispo. In the documentation there is a tutorial on, see: http://docs.oracle.com/javase/tutorial/jdbc/basics/

Browser other questions tagged

You are not signed in. Login or sign up in order to post.