09 · Spring Framework and the IoC container / LESSON 44 OF 102
JdbcTemplate and exception translation
JdbcTemplate removes repetitive JDBC resource handling while keeping SQL visible.
Before this lesson: AOP, proxies, and self-invocation
Understand the topic
Inject a configured DataSource or JdbcTemplate. Provide SQL and parameter values, then map each row through a RowMapper. Spring manages resource cleanup and translates SQLExceptions into its data-access exception hierarchy.
queryForObject expects a result contract, usually exactly one row; use query for zero-to-many rows. A template call does not make several separate operations atomic by itself. Later transaction lessons place related calls inside a managed transaction.
EXAMPLE · JAVA
TaskReader.java
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public class TaskReader {
private final JdbcTemplate jdbc;
public TaskReader(JdbcTemplate jdbc) { this.jdbc = jdbc; }
public List<String> titles(long owner) {
return jdbc.query("SELECT title FROM work_items WHERE owner_id = ? ORDER BY id",
(rs, row) -> rs.getString("title"), owner);
}
}What to expect
With Spring JDBC and a configured DataSource, returns only the requested owner’s titles, including an empty list when none exist.
YOUR TURN
Put it into practice
- 01Replace a hand-written JDBC reader with JdbcTemplate.
- 02Preserve owner filtering and test zero, one, and multiple rows.
- 03Verify: Behavior remains the same while manual connection and ResultSet cleanup disappear from the DAO.
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
JdbcTemplate removes repetitive JDBC resource handling while keeping SQL visible.
- 01Inject a configured DataSource or JdbcTemplate. Provide SQL and parameter values, then map each row through a RowMapper. Spring manages resource cleanup and translates SQLExceptions into its data-access exception hierarchy.
- 02queryForObject expects a result contract, usually exactly one row; use query for zero-to-many rows. A template call does not make several separate operations atomic by itself. Later transaction lessons place related calls inside a managed transaction.
- 03Trace the example: With Spring JDBC and a configured DataSource, returns only the requested owner’s titles, including an empty list when none exist.
A mistake worth avoiding
queryForObject throws when the query finds no row.
DEBUGGING ROOM
Something broke. Where do you look?
queryForObject throws when the query finds no row.
Before revealing the diagnosis, write down your first investigation step.
CHECK YOUR UNDERSTANDING
Does JdbcTemplate require you to concatenate parameter values into SQL?
BUILD IT YOURSELF
Replace a hand-written JDBC reader with JdbcTemplate.
- 1.Replace a hand-written JDBC reader with JdbcTemplate.
- 2.Preserve owner filtering and test zero, one, and multiple rows.
- 3.Reproduce this failure, then repair it: queryForObject throws when the query finds no row.
Acceptance checklist
Lab completion is your self-report. Knowledge checks do not certify project quality.
RETRIEVE IT FROM MEMORY
Does JdbcTemplate require you to concatenate parameter values into SQL?
Revisit this lesson in three days after a successful check or lab.
Ready for the next step?
Behavior remains the same while manual connection and ResultSet cleanup disappear from the DAO.
Reading is separate from passing a knowledge check or completing a lab.