tags:

views:

294

answers:

3

How can I find that the ResultSet, that I have got by querying a database, is empty or not?

A: 

Calculates the size of the java.sql.ResultSet:

int size =0;
if (rs != null) {
    rs.beforeFirst();
    rs.last();
    size = rs.getRow();
}

(Source)

Dolph
As far as I know, that is a bad idea... first of all, you need to ensure that the result can move backwards, second of all, you take a performance hit when doing that. Much quicker to just use a forward only result set, and use a while loop (like has already been suggested by others here)
Agree... I voted up the accepted answer, which answers the question much more directly.
Dolph
+7  A: 

Do this using rs.next():

while (rs.next())
{
    ...
}

If the result set is empty, the code inside the loop won't execute.

Scott Smith
+4  A: 

Immediately after your execute statement you can have an if statement. For example

ResultSet rs = statement.execute();
if (!rs.next()){
//ResultSet is empty
}
Paul