How to Determine if Empty Resultset Was Returned in Java

ResultSet is a class defined in the standard libraries for the Java programming language. A ResultSet object embodies a set of database records (also called "rows"); typically, that set is the result of querying a database for the rows that satisfy a given condition. Depending on the contents of the database at the time the query is evaluated, there may or may not be rows returned by the query -- that is, the ResultSet may be empty. You can write Java code that determines whether a ResultSet is empty or not.

Instructions

    • 1

      Include the following line at the beginning of your Java program:

      import java.sql;

    • 2

      Compute the value of the ResultSet that you want to test for emptiness, as in the following sample code:

      ResultSet myResultSet = Statement.executeQuery("select * from employees;");

    • 3

      Test whether the ResultSet is empty by attempting to use method "ResultSet.absolute()" to move the cursor to the first row, as in the following sample code:

      if (myResultSet.absolute(1)) {

      System.out.print("The ResultSet object is not empty");

      } else {

      System.out.print("The ResultSet object is empty");

      }

      On any non-empty ResultSet object, row 1 exists; therefore "ResultSet.absolute(1)" returns true if and only if the object is not empty.

Related Searches:

References

Comments

Related Ads

Featured