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
| Category | Description |
|---|---|
| Main Technical Topic | Java JDBC ResultSet Interface |
| Solved Problem | Process, navigate and update query results programmatically |
| User Steps | JDBC connection → Running query → Retrieving ResultSet → Navigating rows → Reading/updating data |
| Technical Summary | ResultSet 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
| Genre | Description |
|---|---|
| TYPE_FORWARD_ONLY | It can only be navigated in the forward direction. It is the default type. |
| TYPE_SCROLL_INSENSITIVE | It moves back and forth; It does not reflect database changes. |
| TYPE_SCROLL_SENSITIVE | It can be slid both forward and backward; Reflects database changes. |
| Concurrency | Description |
|---|---|
| CONCUR_READ_ONLY | Only data reading is possible. |
| CONCUR_UPDATABLE | Row data can be updated in memory and reflected in the database with updateRow(). |
🧭 Important ResultSet Methods
| Category | Example | Description |
|---|---|---|
| Navigation (Navigation) | next(), previous(), absolute() | Moves the cursor to a specific line. |
| Reading (Getter) | getString(), getInt(), getDate() | Reads data from the current row. |
| Updater | updateString(), 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 Management | If ResultSet and Statement are not closed, a resource leak occurs. |
| next() Control | next() returns false when the result is finished. |
| Index vs Name | Columns can be accessed by both index and name. |
| Performance | Avoid unnecessary ResultSet copies. |
| Connection Closing | When the connection closes, the connected ResultSet becomes invalid. |
❓ Frequently Asked Questions (FAQ)
- Why should ResultSet be closed?
ResultSet objects left open consume database resources and reduce performance.
- 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")).
- How to complete the update process?
Methods such as updateString() update memory. The updateRow() call reflects it to the database.
- What does TYPE_FORWARD_ONLY do?
The cursor only moves forward; It is not possible to go back or jump to a specific line.
- 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 🚀

