Spring NamedParameterJdbcTemplate (original) (raw)

Last Updated : 4 May, 2026

NamedParameterJdbcTemplate is a class in the Spring JDBC framework that enhances database access by allowing the use of named parameters instead of traditional ? placeholders. It works as a wrapper around JdbcTemplate and internally delegates execution to it.

Syntax of execute()

public T execute(String sql, SqlParameterSource paramSource, PreparedStatementCallback action)

Step-by-Step Implementation

This section demonstrates how to configure, implement, and run a Spring application using NamedParameterJdbcTemplate to perform database operations in a structured manner.

Step 1: Create Database Table

CREATE TABLE STUDENT (

id INT PRIMARY KEY,

name VARCHAR(45),

department VARCHAR(45)

);

**Step 2: Add Maven Dependencies

org.springframework spring-jdbc 5.3.23
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>5.3.23</version>
</dependency>

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.28</version>
</dependency>

`

**Step 3: Configure Spring Beans (AppConfig.java)

@Configuration @ComponentScan(basePackages = "com.example") public class AppConfig {

@Bean
public DataSource dataSource() {
    DriverManagerDataSource ds = new DriverManagerDataSource();
    ds.setDriverClassName("com.mysql.cj.jdbc.Driver");
    ds.setUrl("jdbc:mysql://localhost:3306/school_db");
    ds.setUsername("dbuser");
    ds.setPassword("securepassword");
    return ds;
}

@Bean
public NamedParameterJdbcTemplate namedParameterJdbcTemplate() {
    return new NamedParameterJdbcTemplate(dataSource());
}

}

`

Step 4: Create Model Class (Student.java)

public class Student { private int id; private String name; private String department;

public Student(int id, String name, String department) {
    this.id = id;
    this.name = name;
    this.department = department;
}

// getters and setters

}

`

Step 5: Create DAO Class (StudentDao.java****)**

@Repository public class StudentDao {

private final NamedParameterJdbcTemplate jdbcTemplate;

@Autowired
public StudentDao(NamedParameterJdbcTemplate jdbcTemplate) {
    this.jdbcTemplate = jdbcTemplate;
}

public void insertStudent(Student student) {
    String sql = "INSERT INTO STUDENT (id, name, department) VALUES (:id, :name, :dept)";

    MapSqlParameterSource params = new MapSqlParameterSource()
            .addValue("id", student.getId())
            .addValue("name", student.getName())
            .addValue("dept", student.getDepartment());

    jdbcTemplate.update(sql, params);
}

public Optional<Student> findById(int id) {
    String sql = "SELECT * FROM STUDENT WHERE id = :id";

    try {
        Student student = jdbcTemplate.queryForObject(
                sql,
                Collections.singletonMap("id", id),
                (rs, rowNum) -> new Student(
                        rs.getInt("id"),
                        rs.getString("name"),
                        rs.getString("department"))
        );
        return Optional.of(student);
    } catch (EmptyResultDataAccessException e) {
        return Optional.empty();
    }
}

}

`


**Step 6: Run the Application

public class StudentApplication { public static void main(String[] args) {

    AnnotationConfigApplicationContext context =
            new AnnotationConfigApplicationContext(AppConfig.class);

    StudentDao dao = context.getBean(StudentDao.class);

    Student student = new Student(1, "Geek", "Computer Science");
    dao.insertStudent(student);

    dao.findById(1).ifPresent(System.out::println);

    context.close();
}

}

`

**Output:

Output

Outputc

Student [id=1, name=Geek, department=Computer Science]

Benefits of NamedParameterJdbcTemplate over JdbcTemplate