CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > JDBC > Processing Results with ResultSet in JDBC

JDBC Tutorial

  • Introduction
  • Setting Environment
  • Connecting to a Database
  • Executing Statement
  • Processing Results with ResultSet
  • CRUD Operations
  • Transactions
  • JDBC Best Practices
  • Simple JDBC Project
  • Advanced JDBC Topics
  • Common JDBC Issues
  • Interview Questions
  • Home

Processing Results with ResultSet in JDBC

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 Β· 3 min read Β· 0 Comments
Share: in X

Learn how to retrieve, navigate, and manipulate query results.

Table of Contents

  • What is a ResultSet?
  • 1. Basic Navigation
    • Key Methods
    • Example: Looping Through Rows
  • 2. Retrieving Data
    • By Column Name
    • By Column Index
    • Supported Data Types
  • 3. Handling NULL Values
  • 4. Scrollable and Updatable ResultSets
  • 5. Best Practices
  • Common Errors
  • Full Example: Advanced ResultSet Usage
  • Summary

What is a ResultSet?

A ResultSet is a Java object that holds data returned from a database query (e.g., SELECT). Think of it as a virtual table with rows and columns. You can:

  • Navigate through rows (forward/backward).
  • Extract data by column name or index.
  • Check for NULL values.

1. Basic Navigation

Key Methods

MethodDescription
next()Move to the next row (returns true if successful).
previous()Move to the previous row (requires scrollable ResultSet).
beforeFirst()Move cursor before the first row.
afterLast()Move cursor after the last row.
absolute(int row)Jump to a specific row (e.g., absolute(3)).

Example: Looping Through Rows

String sql = "SELECT id, name, email FROM students";
try (Statement stmt = conn.createStatement();
     ResultSet rs = stmt.executeQuery(sql)) {

    while (rs.next()) { // Move to next row until end
        int id = rs.getInt("id");
        String name = rs.getString("name");
        String email = rs.getString("email");
        System.out.println(id + ": " + name + " - " + email);
    }
}

2. Retrieving Data

Use getXxx() methods to extract values by column name or index (1-based):

By Column Name

double salary = rs.getDouble("salary"); // Column name "salary"

By Column Index

double salary = rs.getDouble(3); // 3rd column in the ResultSet

Supported Data Types

MethodReturn TypeSQL Type
getInt()intINTEGER, SMALLINT
getDouble()doubleDOUBLE, FLOAT
getString()StringVARCHAR, CHAR
getBoolean()booleanBOOLEAN
getDate()DateDATE

3. Handling NULL Values

If a column allows NULL, use wasNull() to check after retrieval:

String email = rs.getString("email");
if (rs.wasNull()) {
    email = "N/A"; // Handle NULL
}

4. Scrollable and Updatable ResultSets

By default, ResultSet is forward-only. For advanced navigation/modification, specify type when creating the Statement:

// Create scrollable, updatable ResultSet
Statement stmt = conn.createStatement(
    ResultSet.TYPE_SCROLL_INSENSITIVE, 
    ResultSet.CONCUR_UPDATABLE
);

ResultSet rs = stmt.executeQuery("SELECT * FROM students");
rs.absolute(2); // Jump to 2nd row
rs.updateString("name", "Charlie"); // Update value
rs.updateRow(); // Commit changes to the database

For detail example on Scrollable ResultSet and Updatable ResultSet you can visit these link.

5. Best Practices

  1. Use Column Names: Avoid indexes (e.g., getString("name") over getString(2)).
  2. Close Resources: Use try-with-resources for ResultSet, Statement, and Connection.
  3. Check for NULLs: Always validate nullable columns.
  4. Limit Data: Use WHERE clauses to avoid oversized ResultSets.

Common Errors

ErrorSolution
Invalid column nameVerify column names in the query.
ResultSet is closedDon’t close the Statement before processing ResultSet.
Operation not supported for forward-only ResultSetUse scrollable ResultSet.

Full Example: Advanced ResultSet Usage

public class ResultSetDemo {
    public static void main(String[] args) {
        String url = "jdbc:mysql://localhost:3306/jdbc_practice";
        String user = "root";
        String password = "root123";

        try (Connection conn = DriverManager.getConnection(url, user, password);
             Statement stmt = conn.createStatement(
                 ResultSet.TYPE_SCROLL_INSENSITIVE,
                 ResultSet.CONCUR_READ_ONLY
             );
             ResultSet rs = stmt.executeQuery("SELECT * FROM students")) {

            // Move to last row
            if (rs.last()) {
                System.out.println("Total rows: " + rs.getRow());
            }

            // Move back to first row
            rs.beforeFirst();
            while (rs.next()) {
                String name = rs.getString("name");
                System.out.println("Student: " + name);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

Summary

  • ResultSet allows navigation and extraction of query results.
  • Use getXxx() methods to retrieve data safely.
  • Scrollable ResultSet enables bidirectional navigation.

Next Up: Transactions in JDBC β€“ Learn how to group operations for atomicity and consistency!

Related Posts:

  • Scrollable ResultSet in JDBC
  • JDBC Interview Questions: Ace Your Technical Screening
  • MySQL Commands for Developers
  • JDBC Tutorial: The Ultimate Guide to Java Database…
  • What is JDBC: Bridging Java and Databases
  • Updatable ResultSet in JDBC
Tags:javajdbc
Was this article helpful?
← Previous ArticleAdvantages and Disadvantages of Java Bean
Next Article β†’java.io Package Overview

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
Β© 2026 CoderSathi. All rights reserved. Privacy Policy Β· Sitemap