Skip to content
Ameen Abozaid
Planned lessonJNR-026-L02

JdbcClient and JdbcTemplate

  • Lesson 2 of 5
  • Junior
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

run queries and updates without writing connection code.

Planned topics

  • JdbcClient is the modern fluent API (Spring 6.1 and later); JdbcTemplate is the older, still common one.
  • Both open, close, and return connections for you.
  • Use named parameters (:name) instead of positional ? so queries stay readable.
  • .query(...) reads rows; .update(...) runs insert, update, delete and returns the affected row count.
  • A KeyHolder returns generated ids.
Draft outline details

Demonstration

java
int rows = jdbc.sql("update product set price = :price where id = :id")
        .param("price", newPrice)
        .param("id", id)
        .update();

Optional<Product> p = jdbc.sql("select id, name, price from product where id = :id")
        .param("id", id)
        .query(Product.class)
        .optional();

Check your understanding

What does update() return?

Show answer

The number of affected rows.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.