- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Navigate through a ResultSet using a JDBC program?
The next() method of the ResultSet interface moves the pointer/Cursor of the current ResultSet object to the next row from the current position. This method returns a boolean value. If there are no rows next to its current position this method returns false, else it returns true.
Therefore, using this method in the while loop you can iterate the contents of the ResultSet object.
while(rs.next()){ }
Getting the column values of each record
The ResultSet interface (also) provides getter methods (getXXX()) to retrieve values in each column of a row, each getter methods has two variants:
getXXX(int columnIndex): Accepts an integer value representing the index of the column and returns its value.
getXXX(String columnLabel ): Accepts a String value representing the name of the column and returns its value.
You need to use the respective getter method based on the datatype of the column in the table.
while(rs.next()) { System.out.print("Brand: "+rs.getString("Column_Name")+", "); System.out.print("Sale: "+rs.getString("Column_Name ")); ……………………… ……………………… System.out.println(""); }
In the same way if it is a bi-directional ResultSet object you can navigate backwards using the previous() method.
Since the pointer of the ResultSet object is positioned before 1st row by default. To navigate backwards you need to shift the pointer/cursor to the next row after the last and, navigate backwards as:
rs.afterLast(); System.out.println("Contents of the table"); while(rs.previous()) { System.out.print("Brand: "+rs.getString("Mobile_Brand")+", "); System.out.print("Sale: "+rs.getString("Unit_Sale")); System.out.println(""); }
- Related Articles
- What are the methods provided by the ResultSet to navigate through it in JDBC?
- How to update the contents of a ResultSet using a JDBC program?
- How to insert a row into a ResultSet object using JDBC?
- How to delete a row from ResultSet object using JDBC?
- How to get all the column names from a ResultSet using JDBC
- How to find the current row of a ResultSet object using JDBC?
- How to insert rows into a ResultSet in JDBC?
- How to move the pointer of a ResultSet to the default position using JDBC?
- How to get column count in a ResultSet in JDBC?
- How to move the pointer of a ResultSet to the end of the table using JDBC?
- How to get the table name of the current ResultSet using JDBC?
- How to connect to a MongoDB database using a JDBC program?
- How to connect to Derby database using a JDBC program?
- How to connect to HSQLDB database using a JDBC program?
- How to connect to PostgreSQL database using a JDBC program?
