Java Academy Logo

Java Academy

JDBC ResultSet

ResultSet represents the table of data returned by a SQL query. Learn to iterate rows, read column values by name or index, and map results to Java objects.

Reading ResultSet

ResultSet rs = stmt.executeQuery("SELECT id, email, created_at FROM users");
while (rs.next()) {
    int id = rs.getInt("id");
    String email = rs.getString("email");
    LocalDateTime created = rs.getTimestamp("created_at").toLocalDateTime();
}

ResultSet Types

TYPE_FORWARD_ONLY

Default. Can only move forward with next(). Most efficient.

TYPE_SCROLL_INSENSITIVE

Can scroll forward and backward. Not affected by DB changes.

CONCUR_READ_ONLY

Cannot update rows through ResultSet (default).

CONCUR_UPDATABLE

Can insert, update, delete rows via ResultSet methods.

Mapping to Objects

In production, map ResultSet rows to POJOs manually or use frameworks like JPA/Hibernate, jOOQ, or MyBatis that handle mapping automatically. For learning JDBC, manual mapping builds strong fundamentals.

Frequently Asked Questions

Should I use column index or name?

Column names are more readable and resilient to column order changes. Index (1-based) is slightly faster but harder to maintain.

Continue Learning