Lesson 38: Working with ResultSet & Mapping Rows to Objects

08 Aug 2026 1 min Swarnil Singhai

ResultSet is a cursor over query results — real applications map each row into a domain object instead of passing raw ResultSets around.

Real-time example

An order-history feature maps each database row into an Order object the rest of the app already understands.

List<Order> orders = new ArrayList<>();
try (ResultSet rs = ps.executeQuery()) {
    while (rs.next()) {
        orders.add(new Order(
            rs.getString("id"),
            rs.getDouble("amount")));
    }
}

What's happening

rs.next() advances the cursor one row at a time and returns false when exhausted — the classic JDBC read loop.

Map to domain objects at the boundary — don't let raw ResultSets leak into business logic, or the DB schema leaks with it.

Advertisement