JdbcTemplate is the central class of Spring's spring-jdbc module. It runs SQL through plain JDBC but takes over the repetitive parts: acquiring and releasing connections, creating and closing statements, iterating result sets and translating SQLException into Spring's unchecked DataAccessException hierarchy. You supply the SQL and the mapping logic; the template does the rest.
It is a good fit when you want direct SQL control without the weight of an ORM such as Hibernate. Since Spring Framework 6.1 there is also JdbcClient, a fluent API built on the same infrastructure.
With raw JDBC, every query repeats the same try-with-resources blocks and exception handling. JdbcTemplate removes that boilerplate.
| Concern | Raw JDBC | JdbcTemplate |
|---|---|---|
| Connection acquire and release | Your code | Done by the template |
| Statement creation and closing | Your code | Done by the template |
| Result set iteration | Your code | You provide a RowMapper |
| Exceptions | Checked SQLException | Unchecked DataAccessException subclasses |
| Transactions | Manual commit | Joins Spring transactions (@Transactional) |
JdbcTemplate is thread-safe after configuration, so one instance can be shared across the application.
Add spring-boot-starter-jdbc and a driver, configure spring.datasource.*, and Spring Boot creates a JdbcTemplate bean you can inject.
Outside Boot, construct it with a DataSource: new JdbcTemplate(dataSource).
Use query for lists, queryForObject for a single row or value, and a RowMapper to build objects. A record works well as the result type.
queryForObject throws EmptyResultDataAccessException when no row matches and IncorrectResultSizeDataAccessException when more than one does. For a quick "first or none" result, call query(...) and take the first element of the list.
Always pass values as ? parameters, never by concatenating strings, to prevent SQL injection.
update handles INSERT, UPDATE and DELETE and returns the affected row count.
To read a generated key, use KeyHolder:
For many rows at once, batchUpdate sends them in batches and is far faster than a loop of single updates:
NamedParameterJdbcTemplate replaces positional ? with names such as :email, which is clearer for queries with many parameters. JdbcClient (Spring 6.1 and later) offers a fluent style:
Wrap multi-statement work in @Transactional on a service method. JdbcTemplate automatically participates in the current transaction, so all statements commit or roll back together.
No. It runs SQL you write and maps rows with your code. It has no entity tracking, lazy loading or generated SQL.
Use JdbcTemplate or JdbcClient for reporting, bulk work and queries where you want exact SQL. Use JPA when you want object graph mapping and many standard CRUD operations. Both can coexist in one application.
No. The template gets a connection from the DataSource, which is usually a pool, and returns it when the call finishes.
Use bound parameters (? or :name). Never build SQL by concatenating user input.
JdbcTemplate removes JDBC boilerplate while keeping SQL in your hands. It manages connections and statements, maps rows through RowMapper, translates exceptions into DataAccessException, supports batch updates and joins Spring transactions. For named parameters use NamedParameterJdbcTemplate, and on Spring 6.1 or later consider JdbcClient for a fluent API.