How can I find that the ResultSet
, that I have got by querying a database, is empty or not?
views:
294answers:
3
A:
Calculates the size of the java.sql.ResultSet:
int size =0;
if (rs != null) {
rs.beforeFirst();
rs.last();
size = rs.getRow();
}
(Source)
Dolph
2010-02-25 04:53:59
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)
2010-04-19 14:21:06
Agree... I voted up the accepted answer, which answers the question much more directly.
Dolph
2010-04-20 01:49:27
+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
2010-02-25 04:54:05
+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
2010-02-25 05:42:31