ResultSet getFetchSize() doesn’t seem to work?

Tags: , ,



I’m having trouble with the getFetchSize() function.

I simply need to know if the SQL query has returned zero rows.

I’ve tried this simple statement:

if (rs.getFetchSize()==0)
    System.out.println("HEADLINE");

where rs is of the ResultSet type. The above code doesn’t seem to work. It always prints the message whether rs is empty or not.

I checked the SQL query itself and it correctly returned non-empty result when rows existed.

Any thoughts on how I can determine whether the query has returned 0 rows? I googled it and couldn’t find any answer.

Answer

ResultSet.getFetchSize() doesn’t return the number of results! From here:

Standard JDBC also enables you to specify the number of rows fetched with each database round-trip for a query, and this number is referred to as the fetch size

You can iterate across the result set and if you don’t have any entries, then you can determine that you got no results. Why can’t you get the result size immediately ? Because the database is returning you a pointer to its results and it’s relying on you to iterate through that (going back to the server for each row).



Source: stackoverflow