Quick revision
The short answer
To load one customer we write jdbcClient.sql("select id, name from customer where id = :id").param("id", id).query(Customer.class).optional(), which maps columns to the record's components by name and returns Optional.empty() when no row matches. To insert and get the generated key, we create a GeneratedKeyHolder, call .update(keyHolder) on the insert statement, and read the key from the holder. single() insists on exactly one row, list() returns all rows, a RowMapper can replace query(Customer.class), and Spring Boot auto-configures a JdbcClient bean whenever a DataSource exists.
What to remember
In this answer, we used JdbcClient's fluent API to load a customer with sql(...).param(...).query(Customer.class).optional(), which maps columns to a record by name and returns empty when nothing matches. We compared optional(), single() and list(), replaced class mapping with a RowMapper when needed, inserted a customer with update(keyHolder) and learned to name the key column on PostgreSQL, and saw that Spring Boot auto-configures a JdbcClient bean when a DataSource exists.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
To load one customer we write jdbcClient.sql("select id, name from customer where id = :id").param("id", id).query(Customer.class).optional(), which maps columns to the record's components by name and returns Optional.empty() when no row matches. To insert and get the generated key, we create a GeneratedKeyHolder, call .update(keyHolder) on the insert statement, and read the key from the holder. single() insists on exactly one row, list() returns all rows, a RowMapper can replace query(Customer.class), and Spring Boot auto-configures a JdbcClient bean whenever a DataSource exists.
Why this question is asked
JdbcClient, added in Spring Framework 6.1 and auto-configured from Spring Boot 3.2, is the modern way to write plain SQL in Spring. It wraps the same machinery as JdbcTemplate and NamedParameterJdbcTemplate behind one fluent API that reads like the query itself. Interviewers ask for these two operations because they cover the most common needs, reading one row that may not exist and inserting with a database-generated key, and both have small traps. In this answer, we will recap what JdbcClient is, load a customer as an Optional, look at the mapping and result options, insert with a generated key, and see how it is configured in Spring Boot.
1A Quick Recap of JdbcClient
JdbcClient is an interface with a fluent API: we start with sql(...), add parameters with param(...), and finish with either query(...) followed by a result method, or update(...). Parameters can be positional, with ? and param(value), or named, with :name and param("name", value). Underneath, it delegates to NamedParameterJdbcTemplate and JdbcTemplate, so it uses the connection of the current Spring transaction and translates SQLException into Spring's DataAccessException hierarchy exactly as they do.
We get one by injecting the bean Spring Boot creates, or by building it ourselves with JdbcClient.create(dataSource) or JdbcClient.create(jdbcTemplate).
2Loading One Customer as an Optional
The lookup reads almost like the sentence we would say: run this SQL, with this id, map rows to Customer, and give me an Optional.
Notice that a missing row is not an error here, unlike JdbcTemplate's queryForObject, which throws EmptyResultDataAccessException. optional() returns empty for zero rows. If the query somehow returned two rows, optional() would throw IncorrectResultSizeDataAccessException, because the method promises at most one; a lookup by primary key can never do that.
3How query(Customer.class) Maps Rows, and the Other Result Methods
query(Customer.class) picks a row mapper for us. For a record, it uses the canonical constructor and matches column names to component names; for a regular class, it creates the object and sets properties by name. Column names in snake_case, such as created_at, match createdAt. If a component has no matching column, the result depends on the mapper and its version (it may receive null or fail), so it is worth checking the aliases in the SQL against the record. For a single column, query(Long.class) maps the value directly.
When the mapping is not simple, for example combining columns or converting a type, we pass our own RowMapper instead. Then the result methods decide how many rows we expect.
| Method | Zero rows | One row | Several rows |
|---|---|---|---|
optional() | Optional.empty() | Optional of the row | IncorrectResultSizeDataAccessException |
single() | EmptyResultDataAccessException | the row | IncorrectResultSizeDataAccessException |
list() | empty list | list of one | list of all |
set() | empty set | set of one | set of distinct rows |
stream() | empty stream | stream of one | stream; close it after use |
4Inserting a Customer and Getting the Generated Key
When the database generates the primary key, for example with an identity column, we need it back after the insert. JDBC returns it through getGeneratedKeys(), and Spring wraps that in a KeyHolder. We pass a GeneratedKeyHolder to update(), and the driver fills it.
The basic form is update(kh), which works when the driver returns just the key. Notice the PostgreSQL trap: when asked for generated keys without column names, the PostgreSQL driver returns all columns of the inserted row, and KeyHolder.getKey() refuses because it holds more than one value. Naming the key column, update(kh, "id"), makes the driver ask for just that column, and it is clearer on every database. getKeyAs(Long.class) gives us the value in the type we want instead of a Number. Also notice that update() returns the number of affected rows, 1 here, which we can check if the insert might be skipped.
5Configuration in Spring Boot
Spring Boot 3.2 and later auto-configures a JdbcClient bean whenever there is a single DataSource on the context, through its JDBC auto-configuration, next to the JdbcTemplate and NamedParameterJdbcTemplate beans. So adding spring-boot-starter-jdbc and a database driver is enough to inject it. With several data sources, we create one JdbcClient per DataSource ourselves with JdbcClient.create(...) and qualify the beans.
Settings such as fetch size, max rows and query timeout come from the underlying JdbcTemplate, which in Boot can be configured with spring.jdbc.template.* properties. Because the client uses the current transaction's connection, the usual @Transactional rules apply: two updates in one @Transactional method commit together.
6Follow-up questions
When would you choose JdbcClient over JdbcTemplate?
For new code, JdbcClient is usually nicer: one fluent API for positional and named parameters, built-in optional() and single(), and simple class-based mapping. JdbcTemplate is still useful for batch updates, callbacks such as RowCallbackHandler for streaming, and the many existing code paths that use it; the two work together on the same DataSource and transaction.
Can JdbcClient bind a whole object as parameters?
Yes. paramSource(object) takes a record or bean and binds its properties as named parameters, so insert into customer(name, city) values (:name, :city) can be filled from a Customer in one call. There is also params(Map) for a map of values.
What does single() throw, and why prefer optional() for lookups?
single() throws EmptyResultDataAccessException when there is no row and IncorrectResultSizeDataAccessException when there are several. For a lookup where a missing row is a normal outcome, optional() expresses that in the return type and lets the caller decide, for example by returning 404, without using an exception for ordinary flow.
7Conclusion
In this answer, we used JdbcClient's fluent API to load a customer with sql(...).param(...).query(Customer.class).optional(), which maps columns to a record by name and returns empty when nothing matches. We compared optional(), single() and list(), replaced class mapping with a RowMapper when needed, inserted a customer with update(keyHolder) and learned to name the key column on PostgreSQL, and saw that Spring Boot auto-configures a JdbcClient bean when a DataSource exists.
Open with
For the lookup I would inject the JdbcClient that Spring Boot auto-configures, since Boot 3.2, when there is a DataSource, and write jdbcClient.sql("select id, name from customer where id = :id").param("id", id).query(Customer.class).optional(). query with a class maps columns to a record's components or a bean's properties by name, with snake_case matching camelCase, and optional gives me Optional.empty when there is no row, so a missing customer is not an exception.
Then, if they want more
If the mapping is more than a name match, I pass a RowMapper lambda instead of the class. I would mention the other result methods too: single throws EmptyResultDataAccessException for no row and IncorrectResultSizeDataAccessException for more than one, and list returns all rows.
For the insert, I create a GeneratedKeyHolder, run jdbcClient.sql("insert into customer(name) values (:name)").param("name", name).update(keyHolder), and read the key with keyHolder.getKeyAs(Long.class). One detail I would point out: on PostgreSQL, if I do not name the key column, the driver returns every column of the inserted row and getKey complains about multiple keys, so I would pass the column name, update(keyHolder, "id").
Finally, JdbcClient sits on top of JdbcTemplate and NamedParameterJdbcTemplate, so it joins the current Spring transaction and translates SQL errors into DataAccessException just like they do.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Spring Boot 3.2, Spring Framework 6
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- java.sql, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Using Spring's JdbcClient (Spring Framework 6.1+), write code that loads one Customer by...": https://scrivio-interview-library.netlify.app/databases/jdbc/109e972c49/