Nearby lessons

13 of 37

JDBC - The ResultSet

📌 What You Will Learn
  • The difference between Select and Non-Select operations
  • executeQuery vs executeUpdate — when to use which
  • Reading results with the ResultSet methods
  • Complete CRUD programs: Insert, Select, Update, Delete
  • The ResultSet object and how it works

The ResultSet is a fundamental part of database programming with JDBC. This lesson explains The ResultSet — How to Read It, ResultSet Types — How You Can Move Through Rows and Program: a scrollable ResultSet in action with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

The ResultSet — How to Read It

A ResultSet is the object that holds the rows returned by a SELECT. Think of it as a table with a cursor that starts before the first row:

In simple words: A ResultSet cursor starts before the first row, not on it. Every next() call moves the cursor one row forward, and when there are no rows left it returns false.
  • rs.next() — moves the cursor to the next row. Returns false when there are no more rows.
  • rs.getInt(columnNumber) / rs.getString(columnNumber) — read a value by column position (starts at 1).
  • rs.getInt("columnName") — read a value by column name (cleaner).

Common getXXX methods: getInt, getString, getDouble, getFloat, getLong, getBoolean, getDate, getTimestamp, getObject.

Example01
JCode Cell
1while (rs.next()) {
2 int id = rs.getInt(1); // first column
3 String name = rs.getString("name"); // by column name
4 double salary = rs.getDouble("salary");
5 System.out.println(id + " " + name + " " + salary);
6}

ResultSet Types — How You Can Move Through Rows

By default, a ResultSet can only move forward (rs.next()). JDBC supports three types and two concurrency modes:

ResultSet typeWhat it allows
TYPE_FORWARD_ONLYOnly forward movement (default)
TYPE_SCROLL_INSENSITIVEMove forward AND backward; does not see changes made by others
TYPE_SCROLL_SENSITIVEScrollable; sees changes made by others (rarely used)
In simple words: A scrollable ResultSet can move forward and backward through the rows and jump anywhere with absolute(n) — but only if you ask for TYPE_SCROLL_INSENSITIVE when creating the Statement.
ConcurrencyWhat it allows
CONCUR_READ_ONLYOnly reading (default)
CONCUR_UPDATABLECan update rows through the ResultSet

Program: a scrollable ResultSet in action

Scrollable ResultSet methods: first(), last(), absolute(n), relative(n), previous(), beforeFirst(), afterLast().

Example03
JCode Cell
1import java.sql.*;
2 
3public class ScrollableDemo {
4 public static void main(String[] args) throws Exception {
5 Connection con = DriverManager.getConnection(
6 "jdbc:mysql://localhost:3306/company", "root", "password");
7 
8 // create a scrollable, read-only ResultSet
9 Statement st = con.createStatement(
10 ResultSet.TYPE_SCROLL_INSENSITIVE,
11 ResultSet.CONCUR_READ_ONLY);
12 ResultSet rs = st.executeQuery("SELECT * FROM employee");
13 
14 rs.afterLast(); // go after the last row
15 System.out.println("Backward: ");
16 while (rs.previous()) { // move backward
17 System.out.println(" " + rs.getInt(1) + " " + rs.getString(2));
18 }
19 
20 rs.absolute(2); // jump directly to row 2
21 System.out.println("Row 2: " + rs.getInt(1) + " " + rs.getString(2));
22 
23 rs.last(); // go to last row
24 System.out.println("Last row: " + rs.getInt(1) + " " + rs.getString(2));
25 con.close();
26 }
27}
Output
Backward: 102 Priya 101 Rahul Row 2: 102 Priya Last row: 102 Priya
📝 Key Takeaways
  • Select ops (SELECT) use executeQuery() → ResultSet.
  • Non-select ops (INSERT/UPDATE/DELETE) use executeUpdate() → row count.
  • rs.next() moves the cursor; getInt/getString/getDouble read values.
  • CRUD = Create(INSERT), Retrieve(SELECT), Update, Delete.
  • Always close the connection after work.
  • Use PreparedStatement for real applications (SQL injection safety).

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1