Loading published curriculum…

08 · SQL and JDBC / LESSON 34 OF 102

ResultSet mapping and generated keys

A ResultSet is a cursor over rows, not a collection you can use after closing the query.

Before this lesson: PreparedStatement, parameters, and injection prevention

Understand the topic

Call next before reading columns and continue until it returns false. Map rows into application values while the result set is open. Use explicit column labels so joins and schema changes do not silently alter positional reads.

getLong returns zero for SQL NULL unless you check wasNull; typed getObject can represent nullable values. For generated identifiers request generated keys when preparing the insert and check that a key row actually exists.

EXAMPLE · JAVA

JDBC mapping fragment

record TaskRow(long id, String title) {}
// Inside a method with Connection c.
try (var ps = c.prepareStatement("SELECT id, title FROM work_items ORDER BY id");
     var rs = ps.executeQuery()) {
 while (rs.next()) {
  var row = new TaskRow(rs.getLong("id"), rs.getString("title"));
  System.out.println(row);
 }
}

What to expect

Prints each mapped row while resources are open; an empty query prints nothing.

YOUR TURN

Put it into practice

  1. 01Return a List<TaskRow> from a DAO method.
  2. 02Add an insert method that retrieves its generated ID and then reads the row back.
  3. 03Verify: Empty results return an empty list and no ResultSet escapes its resource scope.

Pass a quiz or complete a lab to check today’s streak. Each lesson counts once per day, including when you review it later.

UNDER THE HOOD

Build the mental model

A ResultSet is a cursor over rows, not a collection you can use after closing the query.

  1. 01Call next before reading columns and continue until it returns false. Map rows into application values while the result set is open. Use explicit column labels so joins and schema changes do not silently alter positional reads.
  2. 02getLong returns zero for SQL NULL unless you check wasNull; typed getObject can represent nullable values. For generated identifiers request generated keys when preparing the insert and check that a key row actually exists.
  3. 03Trace the example: Prints each mapped row while resources are open; an empty query prints nothing.

A mistake worth avoiding

Reading a query result throws a closed ResultSet error.

DEBUGGING ROOM

Something broke. Where do you look?

Reading a query result throws a closed ResultSet error.

Before revealing the diagnosis, write down your first investigation step.

CHECK YOUR UNDERSTANDING

Where is a ResultSet cursor positioned before the first next call?

Choose one answer

BUILD IT YOURSELF

Return a List<TaskRow> from a DAO method.

  1. 1.Return a List<TaskRow> from a DAO method.
  2. 2.Add an insert method that retrieves its generated ID and then reads the row back.
  3. 3.Reproduce this failure, then repair it: Reading a query result throws a closed ResultSet error.

Acceptance checklist

Lab completion is your self-report. Knowledge checks do not certify project quality.

RETRIEVE IT FROM MEMORY

Where is a ResultSet cursor positioned before the first next call?

Revisit this lesson in three days after a successful check or lab.

Ready for the next step?

Empty results return an empty list and no ResultSet escapes its resource scope.

Reading is separate from passing a knowledge check or completing a lab.

Go deeper · official references