Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Databases
Expertise: Beginner
Feb 18, 2000



Building the Right Environment to Support AI, Machine Learning and Deep Learning

ResultSet Positioning

How can I access the next and previous rows in a ResultSet when I connect to a database through a JDBC driver?

The JDBC 1.0 API only allows you to move forward through a ResultSet with the next() method. The next() will set the ResultSet to point to the next row and return true if that row exists. If there is no other row, then it returns false, allowing you to iterate over the results of a query with a while loop.

The JDBC 2.0 API added the ability to move backward through a ResultSet as well as forward. This feature is not necessarily supported by all drivers. Backward movement is performed with the previous() methods which does exactly the same thing as next(), except it fetches the previous row rather than the next. JDBC 2.0 also adds support for relative positioning with the relative(int rows) method, which will move the result cursor the indicated number of rows from the current position where a negative number means to move backward.

DevX Pro
Thanks for your registration, follow us on our social networks to keep up-to-date