The ResultSet interface in JDBC represents the rows returned by a SQL query. It is a cursor over the result: you move it row by row with next() and read each column with typed getters such as getInt() and getString(). Without it, a Java program has no way to consume the data a SELECT returns.
A ResultSet is tied to the Statement that created it and to an open database resource, so it must be closed. The modern way is try-with-resources, which closes the ResultSet, Statement and Connection in the correct order even when an exception is thrown.
When you call executeQuery() on a Statement or PreparedStatement, the driver returns a ResultSet positioned before the first row. The result is not necessarily loaded into memory: many drivers fetch rows in batches as you call next(), which is why the connection must stay open while you read.
The interface gives you four things:
ResultSetMetaData.The standard pattern is a PreparedStatement with bound parameters, a while (rs.next()) loop and automatic cleanup.
The cursor starts before the first row, so you must call next() before reading anything. next() returns false when there are no more rows, which ends the loop. Column labels are case-insensitive, and column indexes start at 1, not 0.
getObject(column, Class) (JDBC 4.1) converts to a modern java.time type directly, which is preferable to the legacy java.sql.Timestamp. For nullable columns, call rs.wasNull() after reading a primitive, or use getObject(column, Integer.class), which returns null for SQL NULL.
The default ResultSet is forward-only and read-only. You choose a different behaviour when creating the statement.
| Constant | Meaning |
|---|---|
TYPE_FORWARD_ONLY | Cursor moves forward only. Default and the most efficient. |
TYPE_SCROLL_INSENSITIVE | Scrollable; does not see changes made by others after the query ran. |
TYPE_SCROLL_SENSITIVE | Scrollable; may reflect changes made by others. Driver support varies. |
CONCUR_READ_ONLY | Rows cannot be modified through the result set. Default. |
CONCUR_UPDATABLE | Rows can be updated, inserted or deleted through the result set. |
Scrollable methods such as previous(), first(), last() and absolute(int) throw SQLException on a forward-only result set. Scrollable cursors cost memory or round trips on many databases, so use them only when you really need random access.
If the statement was created with CONCUR_UPDATABLE and the query is simple enough (usually a single table that includes the primary key), you can change rows through the cursor.
updateRow() writes the change to the database; cancelRowUpdates() discards it. moveToInsertRow() with insertRow() adds a row, and deleteRow() removes one. In practice, most applications prefer an explicit UPDATE statement, because updatable result sets are driver dependent and hold the cursor open.
ResultSetMetaData describes the shape of a result, which is useful for generic tools, exports and debugging.
Use getColumnLabel() rather than getColumnName() when the query uses aliases, because the label returns the AS name.
SELECT *.setFetchSize(n) on the statement to hint how many rows the driver should fetch per round trip. Some drivers, PostgreSQL among them, only stream when auto-commit is off and a fetch size is set.ResultSet. Closing the Statement closes its result sets, and closing the Connection returns it to the pool when you use a pool.ResultSet after the connection is closed; copy the data into records or DTOs first.A record makes the mapping step short and immutable:
A RowSet extends ResultSet and can be disconnected from the database, serialised and used as a JavaBean. A plain ResultSet stays tied to an open connection.
No. Column indexes start at 1. Calling getString(0) throws an SQLException.
The ResultSet was closed, often because you re-executed the same Statement, or because the Statement or Connection closed first. Finish reading each result before reusing the statement.
Names are clearer and survive column reordering; indexes are marginally faster. Prefer names unless you profile a hot loop.
The ResultSet interface is the JDBC cursor over query results. Read it with next() and typed getters, open it in try-with-resources, and choose scrollable or updatable modes only when needed. Use ResultSetMetaData for column details, map rows into records before the connection closes, and prefer explicit SQL updates over updatable result sets.