Sunday 9 October 2016

How to check if ResultSet is empty in JDBC - Java Example

The JDBC ResultSet doesn't provide any isEmpty(), length() or size() method to check if its empty or not. Hence, when a Java programmer needs to determine if ResultSet is empty or not, it just calls the next() method and if next() return false it means ResultSet is empty. This is correct but the main problem with this approach is if the ResultSet is not empty then you may miss the first row if you follow the standard idiom to iterate over ResultSet and get the rows which involve calling next() method of ResultSet in a while loop. The key thing, which you need to remember is that initially the ResultSet's cursor points to before the first row when you call the next() method it points to the first row and if you don't get this data and calls the next() method again then you will lose the first row.
Read more »

No comments:

Post a Comment