Resultset has no method for hasNext. I want to check if the resultSet has any value
is this the correct way
if (!resultSet.next() ) {
System.out.println("no data");
}
Resultset has no method for hasNext. I want to check if the resultSet has any value
is this the correct way
if (!resultSet.next() ) {
System.out.println("no data");
}
I think the easiest way for checking result set is via CollectionUtils under package org.apache.commons.collections.CollectionUtils
This will check for null as well as empty result set condition.
For more detail information you can refer to the following doc. CollectionUtils
You would usually do something like this:
If you want to report an empty set, add a variable counting the items read. If you only need to read a single item, then your code is adequate.
isAfterLast()
also returns false for empty result set but since cursor is before first row anyways, this method seems more clear.I created the following method to check if a ResultSet is empty.
It is very important to have the following considerations:
CallableStatement object must be setted to let to ResultSet object go at the end and go back to top.
TYPE_SCROLL_SENSITIVE: ResultSet object can shift at the end and go back to top. Further can catch last changes.
CONCUR_READ_ONLY: We can read the ResultSet object data, but can not updated.
Initially, the result set object (rs) points to the BFR (before first record). Once we use rs.next(), the cursor points to the first record and the rs holds "true". Using the while loop you can print all the records of the table. After all the records are retrieved, the cursor moves to ALR (After last record) and it will be set to null. Let us consider that there are 2 records in the table.
In short hand, we can also write the condition as while (rs.next()).
you can do something like this