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):
for(i=1; i<=n; i++) {
rs.absolute(i)
// 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
As to the concrete question about rs. next() , it shifts the cursor to the next row of the result set from the database and returns true if there is any row, otherwise false .
The next method moves the cursor to the next row, and because it returns false when there are no more rows in the ResultSet object, it can be used in a while loop to iterate through the result set. A default ResultSet object is not updatable and has a cursor that moves forward only.
This method returns a boolean value specifying whether the ResultSet object contains more rows. If there are no rows next to its current position this method returns false, else it returns true. Using this method in the while loop you can iterate the contents of the result set.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With