Support Online
Skip to main content

Java ResultSet Guide: Managing Database Query Results

📘 What Will You Learn in This Guide?

In this guide, you will learn in detail the java.sql.ResultSet interface, which forms the basis of the Java JDBC framework.
ResultSet object; It is used to read the results of database queries, navigate through records, and update data.
You will also get to know the different ResultSet types (TYPE_SCROLL_INSENSITIVE etc.) and concurrency modes (CONCUR_UPDATABLE etc.).


🧠 Phase 1 – Technical Summary

CategoryDescription
Main Technical TopicJava JDBC ResultSet Interface
Solved ProblemProcess, navigate and update query results programmatically
User StepsJDBC connection → Running query → Retrieving ResultSet → Navigating rows → Reading/updating data
Technical SummaryResultSet is the core component of JDBC. It reads data on a row basis, provides control with the cursor and allows read/update operations.

⚙️ How Does ResultSet Work?

ResultSet works with the **cursor) logic on the records returned from the database query.
The cursor is initially positioned before the first line. When the next() method is called, it advances to the next line and the data there is read with getter methods such as getString(), getInt().


💻 Using ResultSet Step by Step

1️⃣ Database Preparation (MySQL Example)

CREATE DATABASE genixnode_db;
USE genixnode_db;

CREATE TABLE personel (
personel_id INT PRIMARY KEY,
ad VARCHAR(32),
soyad VARCHAR(32),
dogum_tarihi DATE
);

INSERT INTO personel VALUES (1, 'Ali', 'Yilmaz', '1998-11-11');
INSERT INTO personel VALUES (2, 'Veli', 'Can', '1988-10-22');
INSERT INTO personel VALUES (3, 'Ayse', 'Kara', '1999-05-11');

💡 These SQL commands create a sample employee table.


2️⃣ JDBC Connection and Query Processing


package com.genixnode.db;

import java.sql.*;

public class ResultSetDemo {
public static void main(String[] args) {
String sorgu = "SELECT personel_id, ad, soyad, dogum_tarihi FROM personel";
try (Connection conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/genixnode_db", "root", "genixnode_sifre");
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sorgu)) {

while (rs.next()) {
int id = rs.getInt("personel_id");
String ad = rs.getString("ad");
String soyad = rs.getString("soyad");
Date dogum = rs.getDate("dogum_tarihi");
System.out.printf("ID:%d | Ad:%s | Soyad:%s | Doğum:%s%n", id, ad, soyad, dogum);
}

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

💡 This example prints all records in the database to the console.


🔄 ResultSet Types and Concurrency Modes

GenreDescription
TYPE_FORWARD_ONLYIt can only be navigated in the forward direction. It is the default type.
TYPE_SCROLL_INSENSITIVEIt moves back and forth; It does not reflect database changes.
TYPE_SCROLL_SENSITIVEIt can be slid both forward and backward; Reflects database changes.
ConcurrencyDescription
CONCUR_READ_ONLYOnly data reading is possible.
CONCUR_UPDATABLERow data can be updated in memory and reflected in the database with updateRow().

🧭 Important ResultSet Methods

CategoryExampleDescription
Navigation (Navigation)next(), previous(), absolute()Moves the cursor to a specific line.
Reading (Getter)getString(), getInt(), getDate()Reads data from the current row.
UpdaterupdateString(), updateInt(), updateRow()It changes the data in memory and makes it permanent with updateRow().
Other (Misc.)close(), getMetaData()Closes resources and returns table metadata.

✍️ Row Update Example


Statement stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
ResultSet rs = stmt.executeQuery("SELECT * FROM personel");

if (rs.absolute(2)) {
System.out.println("Mevcut isim: " + rs.getString("ad"));
rs.updateString("ad", "Hasan");
rs.updateRow();
}

💡 This code updates the “name” value in the second row to “Hasan”.


💡 Points to Consider

💡 Status🧾 Description
Resource ManagementIf ResultSet and Statement are not closed, a resource leak occurs.
next() Controlnext() returns false when the result is finished.
Index vs NameColumns can be accessed by both index and name.
PerformanceAvoid unnecessary ResultSet copies.
Connection ClosingWhen the connection closes, the connected ResultSet becomes invalid.

❓ Frequently Asked Questions (FAQ)

  1. Why should ResultSet be closed?

ResultSet objects left open consume database resources and reduce performance.

  1. What is used to get column data?

If you want, you can use the index (rs.getInt(1)) or the column name (rs.getInt("personnel_id")).

  1. How to complete the update process?

Methods such as updateString() update memory. The updateRow() call reflects it to the database.

  1. What does TYPE_FORWARD_ONLY do?

The cursor only moves forward; It is not possible to go back or jump to a specific line.

  1. From which objects is ResultSet obtained?

Statement can be created with PreparedStatement and CallableStatement objects.


🏁 Conclusion

ResultSet is one of the most fundamental components of JDBC. Understanding how to read query results line by line, update them when necessary, and manage memory is of great importance in database applications. You can try these examples immediately on your own database server that you will install on the GenixNode platform 🚀