9

Possible Duplicate:
How to check if resultset has one row or more?

What will executeQuery(String sql) return when the result of the SQL Query is zero rows>? If it returns a ResultSet Object, how will we detect that the SQL Query has returned nothing.

Assume the SQL Query to be a SELECT statement.

Community
  • 1
  • 1
Devashish Dixit
  • 1,153
  • 6
  • 16
  • 25
  • Did you try that? should return or Empty resultSet – jmj Jun 13 '12 at 12:35
  • @JigarJoshi how will we detect that the returned object contains zero rows? – Devashish Dixit Jun 13 '12 at 12:35
  • [`resultSet.next()`](http://docs.oracle.com/javase/1.4.2/docs/api/java/sql/ResultSet.html#next%28%29) returns `false` in case there is no more data – jmj Jun 13 '12 at 12:36
  • The best way I found was in the second part of [this](http://stackoverflow.com/questions/867194/java-resultset-how-to-check-if-there-are-any-results) SO answer where he does the check without using `next()`. Btw, yes, it returns a ResultSet object. – vellvisher Jun 13 '12 at 12:40

7 Answers7

17

Did you check ResultSet's next method . Initially the ResultSet's cursor points to before the first row, the very first call to next() returns false implies there was no data in the ResultSet. See How do I get the size of a ResultSet? , since there is no direct size() or length() method for Resultsets in Java.

Community
  • 1
  • 1
Sandeep Pathak
  • 10,567
  • 8
  • 45
  • 57
4

The next() method of the resultSet moves the cursor to the next row and returns a boolean that indicates if a data has been read or not. Usually it's used with a while loop

while (myresultset.next()){ //some statement; } In your case the fist call of the next method will return false if no data matche the query.

jocelyn
  • 788
  • 6
  • 12
3

it will return a ResultSet. Use

boolean hasResult = rs.next();

to find out if there is a result row.

Baz
  • 36,440
  • 11
  • 68
  • 94
1

Empty resultSet with zero rows i.e. resultSet.next() will return false.

manurajhada
  • 5,284
  • 3
  • 24
  • 43
0

If the ResultSet returned is empty, then the output of the first invocation of the next() method on this ResultSet will return false.

Basically the usual procedure is something among the lines

ResultSet rs = executeQuery("select ....");
while(rs.next()) {
    //do something with the results
}
Ilya Saunkin
  • 18,934
  • 9
  • 36
  • 50
0

It will return empty ResultSet if the query fetches 0 records. Initially the cursor points to before resultset, when ever we call resultset.next() cursor moves to next resultset (Iterator patter) and return true if there are any results exists, otherwise it just returns false. In this case the first call to next() method returns false.

RP-
  • 5,827
  • 2
  • 27
  • 46
-1

You can use the rs.getRow() method.

 if(rs.getRow() >= 1){
      //Has at least 1 result
    }

Source:

/**
    * Retrieves the current row number.  The first row is number 1, the
    * second number 2, and so on.
    * <p>
    * <strong>Note:</strong>Support for the <code>getRow</code> method
    * is optional for <code>ResultSet</code>s with a result
    * set type of <code>TYPE_FORWARD_ONLY</code>
    *
    * @return the current row number; <code>0</code> if there is no current row
    * @exception SQLException if a database access error occurs
    * or this method is called on a closed result set
    * @exception SQLFeatureNotSupportedException if the JDBC driver does not support
    * this method
    * @since 1.2
 */
      int getRow() throws SQLException;
Akhi
  • 2,242
  • 18
  • 24