Skip to content
Ameen Abozaid
JNR-026

Spring JDBC

  • 5 Lessons
  • 2.5 h practice (estimate)
On this page 8 topics

Overview

Topics

  • JdbcTemplate
  • parameter binding
  • mapping query results

Learn first

2.5 h of practice (estimate)

Learning outcomes

  1. Configure a DataSource through Spring Boot properties.
  2. Query and update with JdbcClient/JdbcTemplate and named parameters.
  3. Map rows to records with RowMapper or automatic mapping.
  4. Explain how Spring translates SQLExceptions into its DataAccessException hierarchy.

Lessons

  1. 01DataSource configuration in Boot
  2. 02JdbcClient and JdbcTemplate
  3. 03Mapping rows
  4. 04Exception translation
  5. 05Demo: replacing the in-memory repository with Spring JDBC

Lab and exercises

Lab

JNR-026-LAB1 · Products on PostgreSQL

Outcomes: 1–4 · Time: ~2.5 h

  1. Add the JDBC starter and driver; configure the datasource in application.yml with credentials from environment variables.
  2. Implement ProductRepository (find by id, list, save, update, delete) with JdbcClient and named parameters, behind the existing interface.
  3. Use one RowMapper shared by all queries.
  4. Translate a duplicate SKU to a domain exception in the repository.
  5. Write an integration test against a real PostgreSQL database.

Acceptance: no controller or service changes needed; integration test passes against a real database.

Exercises (assessment: implement a reporting query repository method)

IDOutcomeTask
JNR-026-EX011Write the application.yml datasource block without a hard-coded password.
JNR-026-EX022Write update and delete methods with named parameters and return whether a row was affected.
JNR-026-EX033Map a product row to a record with a RowMapper, then do it again with query(Product.class).
JNR-026-EX044Name the exception Spring throws for a unique violation and for .single() with no row.
JNR-026-EX052, 3Reporting method salesByCategory() returning record CategoryTotal(String category, BigDecimal total) from a group by query.
JNR-026-EX064Explain why catching DataAccessException in every repository method is a mistake.
Show solutions
  • EX01 url: jdbc:postgresql://localhost:5432/shop, username: ${DB_USER}, password: ${DB_PASSWORD}.
  • EX02 int n = jdbc.sql("update product set price = :p where id = :id").param("p", p).param("id", id).update(); return n == 1;
  • EX03 (rs, i) -> new Product(rs.getLong("id"), rs.getString("name"), rs.getBigDecimal("price")) versus jdbc.sql(sql).query(Product.class).list().
  • EX04 DuplicateKeyException (a DataIntegrityViolationException); EmptyResultDataAccessException.
  • EX05 select c.name as category, sum(p.price) as total from product p join category c on c.id = p.category_id group by c.name mapped with .query(CategoryTotal.class).list().
  • EX06 It hides real failures, repeats code, and leaves nobody able to decide; catch a specific subclass where you can act and let the rest reach the global handler.

Quiz

Outcomes: O1 DataSource configuration · O2 JdbcClient/JdbcTemplate · O3 row mapping · O4 exception translation.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1Boot's default connection pool is:O1
  2. 2Where should the database password come from?O1
  3. 3Named parameters look like:O2
  4. 4JdbcClient.update() returns:O2
  5. 5Spring's JDBC helpers take care of:O2
  6. 6A RowMapper maps:O3
  7. 7Where should ResultSet handling live?O3
  8. 8A unique-key violation becomes:O4
  9. 9DataAccessException is:O4
  10. 10Best practice for handling it:O4

Practical part

Six exercises JNR-026-EX01 to EX06 plus the lab; the assessment is implementing a reporting query repository method (EX05). Pass: at least five exercises correct and the lab integration test green.

Common mistakes

Leaking ResultSet handling into services; catching DataAccessException everywhere.

Assessment

Implement a reporting query repository method.

Career relevance

JDBC remains the right choice for reporting and bulk work (MID-022).

Type to search levels, phases, modules and lessons.