JdbcClient and JdbcTemplate
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
JdbcClientis the modern fluent API (Spring 6.1 and later);JdbcTemplateis 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
KeyHolderreturns generated ids.
Draft outline details
Demonstration
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.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.