The ResultSet interface in JDBC is used to retrieve and navigate through the data returned by a SQL SELECT query. When a query is executed using executeQuery(), it returns a ResultSet object containing the retrieved rows. The next() method moves the cursor from one row to the next, while methods such as getInt(), getString(), and getDouble() retrieve column values. ResultSet also provides navigation methods such as first(), last(), previous(), and absolute() for moving the cursor to different rows.
Program: Retrieving and Navigating Query Results Using ResultSet
This program retrieves employee records from the employee table and navigates through the result set using next(), first(), last(), previous(), and absolute(). A scrollable ResultSet is created using TYPE_SCROLL_INSENSITIVE so that the cursor can move both forward and backward.
import java.sql.*;
public class ResultSetNavigationExample {
public static void main(String[] args) {
String url = "jdbc:mysql://localhost:3306/jdbc_demo";
String username = "root";
String password = "password";
String sql = "SELECT employee_id, employee_name, " +
"department, salary FROM employee";
try {
Connection con =
DriverManager.getConnection(
url, username, password);
Statement stmt = con.createStatement(
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY
);
ResultSet rs = stmt.executeQuery(sql);
// Navigate to the first record
if (rs.first()) {
System.out.println("First Employee");
System.out.println("----------------------");
displayEmployee(rs);
}
// Navigate to the next record
if (rs.next()) {
System.out.println("\nNext Employee");
System.out.println("----------------------");
displayEmployee(rs);
}
// Navigate to the last record
if (rs.last()) {
System.out.println("\nLast Employee");
System.out.println("----------------------");
displayEmployee(rs);
}
// Navigate to the previous record
if (rs.previous()) {
System.out.println("\nPrevious Employee");
System.out.println("----------------------");
displayEmployee(rs);
}
// Navigate to the third record
if (rs.absolute(3)) {
System.out.println("\nThird Employee");
System.out.println("----------------------");
displayEmployee(rs);
}
rs.close();
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
public static void displayEmployee(ResultSet rs)
throws SQLException {
System.out.println(
"Employee ID: " +
rs.getInt("employee_id"));
System.out.println(
"Employee Name: " +
rs.getString("employee_name"));
System.out.println(
"Department: " +
rs.getString("department"));
System.out.println(
"Salary: " +
rs.getDouble("salary"));
}
}Execution and Output:
PS D:\test> javac -classpath ".;D:\test\mysql-connector.jar" ResultSetNavigationExample.java
PS D:\test> java -classpath ".;D:\test\mysql-connector.jar" ResultSetNavigationExample
First Employee
----------------------
Employee ID: 101
Employee Name: Ravi
Department: IT
Salary: 50000.0
Next Employee
----------------------
Employee ID: 102
Employee Name: Priya
Department: HR
Salary: 48000.0
Last Employee
----------------------
Employee ID: 106
Employee Name: Chakrapani
Department: Finance
Salary: 200000.0
Previous Employee
----------------------
Employee ID: 105
Employee Name: Mahesh
Department: null
Salary: 200000.0
Third Employee
----------------------
Employee ID: 104
Employee Name: Arun
Department: null
Salary: 38000.0Code language: CSS (css)
