Take the 2-minute tour ×

Suppose to have a ResultSet rs with n object.

This code:

while(rs.next()) {
   // do something on rs

is algoritmically equal to this code (i.e. both gave the same result):

   // do something on rs

But are this equivalant on terms of throughouts? Is the first faster? Or, for a given i, rs.next() is just a wrapper for rs.absolute(i+1)?

Best regards MC

share|improve this question
What stops you from testing it yourself? Most likely, the difference is negigible so the code that is easier to read should be used. –  Klas Lindbäck Dec 19 '12 at 14:32
@KlasLindbäck Writing a correct microbenchmark is surprisingly nontrivial. –  Michael McGowan Dec 19 '12 at 15:03
With rs.next() you don't need to know the result set size beforehand, with rs.absolute() you need to. Which means a second query beforehand. –  BalusC Dec 19 '12 at 15:04
@KlasLindbäck It seemed an intersting question, with a good margin of debate. –  marianoc84 Dec 19 '12 at 15:10

2 Answers 2

up vote 6 down vote accepted

rs.next demands a simpler kind of database cursor (FORWARD_ONLY) than rs.absolute so in most cases you will degrade performance/resource efficiency with rs.absolute. In certain cases, where there is no optimization for a FORWARD_ONLY cursor anyway, you may get the same performance.

Some drivers may allow absolute calls even with FORWARD_ONLY, validating that the requested record is the next one, but again others may throw an exception regardless.

share|improve this answer
So if I declared my PreparedStatement as prepareStatement( SQL, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE ); the two blocks of code are absolutely equals. Right? –  marianoc84 Dec 19 '12 at 14:36
This cannot be answered in the general because these are all just hints to the driver. There is less chance of a difference, true, but I'm sure there are counterexamples as well. –  Marko Topolnik Dec 19 '12 at 14:43
@Mariano there is no way to confirm that, as it will strongly depend on the implementation of the driver and the behavior of the underlying database. In general I'd say: use rs.next() if you want to go through a ResultSet sequentially, but that is just a gut feeling –  Mark Rotteveel Dec 19 '12 at 15:54
@MarkRotteveel I think we can safely say that you can't go wrong with rs.next(): there is practically no chance to have better performance with anything else. –  Marko Topolnik Dec 19 '12 at 18:46
@MarkoTopolnik I agree with you, but as I haven't actually seen the numbers (and I know how vastly different implementations of ResultSet can be), I didn't want to make an absolute statement about all JDBC implementations –  Mark Rotteveel Dec 20 '12 at 8:44

The former rs.next() might be faster, although it depends on the underlying implementation and in reality you might get the same performance due to the internal optimizations.

The driver might not be taking the current position into account when doing rs.absolute() so it might have slightly a little bit overhead, while in rs.next() its only moving forward.

So if you need to iterate through all the results, just use rs.next(), if you need to skip to a specific results use rs.absolute().

Remember that the result set is in fact often being handled remotely by the database (unless its a CachedResultSet), so going to and fro might involve more overheads than you think.

share|improve this answer

Your Answer


