Loading

Quipoin Menu

Learn • Practice • Grow

spring / Spring JdbcTemplate
interview

Q1. What is JdbcTemplate?
JdbcTemplate is a Spring class that simplifies JDBC operations.
It handles resource management (opening/closing connections) and exception handling, reducing boilerplate code.
It provides methods for executing queries, updates, and batch operations.

Q2. How do you configure JdbcTemplate?
Define a DataSource bean and then JdbcTemplate bean, injecting the DataSource.
Example:
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
    return new JdbcTemplate(dataSource);
}

Q3. How do you query for a single object using JdbcTemplate?
Use queryForObject with a RowMapper.
Example:
String sql = "SELECT * FROM users WHERE id = ?";
User user = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), id);

Q4. How do you perform an update using JdbcTemplate?
Use update method.
Example:
String sql = "UPDATE users SET name = ? WHERE id = ?";
jdbcTemplate.update(sql, name, id);

Q5. What is RowMapper?
RowMapper is an interface to map each row of a ResultSet to an object.
You implement mapRow method.
BeanPropertyRowMapper is a convenient implementation that maps columns to bean properties by matching names.