liveBook · Manning

JdbcTemplate

As explained in Test Driven: Practical TDD and Acceptance TDD for Java Developers

JdbcTemplate is a Spring Framework class that provides higher-level JDBC operations while managing repetitive data-access and resource-handling tasks.

Raw JDBC makes data access repetitive: resource handling and nested try-catch code can crowd out the persistence logic. JdbcTemplate takes care of that boilerplate so a DAO can focus on what varies. section 6.2.2

A DAO uses JdbcTemplate to:

  • Run a query by providing SQL, parameter values, and a RowMapper, without handling Connection or PreparedStatement directly. section 6.2.2
  • Convert each result row into an object with a RowMapper, which can be reused for queries involving the same table. section 6.2.2

Find people by last name

A PersonDao query can pass `SELECT * FROM employee WHERE last_name = ?`, the last-name argument, and a PersonRowMapper. That mapper builds a Person from the row’s first and last name. section 6.2.2

Figure 6.3. Collaboration between the template code provided by the Spring Framework and the DAO implementation provided by the developer

See how framework-provided template code handles the shared work while the DAO supplies its specific data-access behavior. section 6.2.2

How Test Driven: Practical TDD and Acceptance TDD for Java Developers covers it

How deep it goes
Explains resource-handling boilerplate, query inputs, and RowMapper responsibilities without detailing JdbcTemplate internals.
Examples
Uses a PersonDao query for employees by last name and a mapper that creates Person objects from result rows.
What you can do after
Apply JdbcTemplate to implement DAO queries while delegating repetitive JDBC resource-handling boilerplate.
How it connects
Connects DAO implementations with JDBC, SQL queries, RowMapper, and the Spring Framework.

Also explained in

Compare how 2 books explain JdbcTemplate →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Test Driven: Practical TDD and Acceptance TDD for Java Developers. The section links point to the parts of the book it draws on.