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");
}
That's correct, initially the
ResultSet
's cursor is pointing to before the first row, if the first call tonext()
returnsfalse
then there was no data in theResultSet
.If you use this method, you may have to call
beforeFirst()
immediately after to reset it, since it has positioned itself past the first row now.It should be noted however, that Seifer's answer below is a more elegant solution to this question.
To be totally sure of rather the resultset is empty or not regardless of cursor position, I would do something like this:
This function will return true if ResultSet is empty, false if not or throw an SQLException if that ResultSet is closed/uninitialized.
Assuming you are working with a newly returned
ResultSet
whose cursor is pointing before the first row, an easier way to check this is to just callisBeforeFirst()
. This avoids having to back-track if the data is to be read.As explained in the documentation, this returns false if the cursor is not before the first record or if there are no rows in the ResultSet.