ResultSet getFetchSize() doesn't seem to work?

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).


The fetch size is the number of rows that should be retrieved from the database in each roundtrip. It has nothing to do with the number of rows returned.

What you should do is call rs.next() - iff your query didn't return any rows it will return false.


Actually, I think another way to do what you are looking for is this:

Class.forName("org.exempleDatabase.Driver");
Connection  conn = DriverManager.getConnection("jdbc:exempleDatabase:/../smartytwiti", "username", "password");
ResultSet resset = conn.createStatement().executeQuery("select count(*) from yourTable;");
resset.last();
int resnum = resset.getRow();
if(resnum<1)System.out.println("HEADLINE");

That will take the ResultSet, move to the last row of the set, and then give the number of that row in resnum. Just remember to move back with resset.beforeFirst() if you are going to iterate through the results with resset.next().

Hope that helps.

Tags:

Sql

Java

Jdbc