• ↑↓ pour naviguer
  • pour ouvrir
  • pour sélectionner
  • ⌘ ⌥ ↵ pour ouvrir dans un panneau
  • ←→ pour naviguer
  • esc pour rejeter
⌘ '
raccourcis clavier

The Problem

Raw JDBC requires manually managing every step of database access: getting a connection, creating a statement, setting parameters, iterating a ResultSet, handling checked SQLExceptions, and closing resources in finally blocks. A simple query becomes 15+ lines of repetitive, error-prone boilerplate.

Core Idea

Spring JDBC Template provides a template-based API that eliminates JDBC boilerplate. JdbcTemplate wraps the JDBC workflow: it handles connection acquisition, statement creation, parameter binding, result extraction, and cleanup. Developers provide only the SQL and the logic to map results to objects.

How It Works

  1. JdbcTemplate: Core class wrapping JDBC — execute query/update with automatic resource management
  2. NamedParameterJdbcTemplate: Uses named parameters (:name) instead of positional (?) — more readable, especially with many parameters
  3. RowMapper: (ResultSet rs, int rowNum) → T maps a result set row to a domain object
  4. ResultSetExtractor: For custom result extraction (e.g., building a Map from multiple rows)
  5. SQL scripts: ResourceDatabasePopulator or ScriptUtils for executing SQL scripts during testing or setup

Visual Explanation

jdbc_template APP Application jdbcTemplate.query(sql, rowMapper) JT JdbcTemplate APP->JT NAMED NamedParameterJdbcTemplate (:param syntax) JT->NAMED BARE Raw JDBC (connection, statement, resultset, exception) JT->BARE ROWM RowMapper<T> (ResultSet → Object) JT->ROWM RSE ResultSetExtractor<T> (custom extraction) JT->RSE DB Database BARE->DB ROWM->BARE RES List<T> / T ROWM->RES RSE->RES

Key Properties

  • JdbcTemplate: Simplest template — positional ? parameters, automatic resource cleanup
  • NamedParameterJdbcTemplate: Named :param parameters, better readability, uses SqlParameterSource
  • SimpleJdbcTemplate (deprecated): Legacy wrapper, replaced by JdbcTemplate
  • RowMapper: Maps individual rows — reusable, no external state
  • ResultSetExtractor: Processes entire ResultSet (multiple rows, custom structures)
  • SQL scripts: Execute SQL files via ResourceDatabasePopulator for setup, testing, or migrations
  • PreparedStatementCallback: For full control over PreparedStatement creation and execution

Connections

  • Built from: JDBC — JdbcTemplate is a wrapper over raw JDBC
  • Built from: Spring Framework — JdbcTemplate is a Spring module for data access
  • Contrasts with: Spring ORM — JdbcTemplate is direct SQL; Spring ORM is object-oriented
  • Contrasts with: Spring Data JPA — JdbcTemplate gives SQL control; JPA abstracts SQL
  • Related: Hibernate Query Language — Both execute database queries; HQL is ORM-based, JdbcTemplate is SQL-based

Edge Cases & Gotchas

  • Large results: JdbcTemplate fetches all results into memory by default — use RowCallbackHandler or streaming for large datasets
  • No caching: Unlike ORM, JdbcTemplate has no built-in caching — each query hits the database
  • No lazy loading: All fields must be explicitly selected; no proxy-based lazy loading
  • SQL injection: Always use parameterized queries (? or :param), never concatenate user input into SQL strings
  • DataSource configuration: JdbcTemplate needs a properly configured DataSource bean; Spring Boot auto-configures one