In this post I will show you how to use NamedParameterJdbcTemplate and BeanPropertySqlParameterSource to execute query. The NamedParameterJdbcTemplate class adds support for programming JDBC statements using named parameters, as opposed to programming JDBC statements using only classic placeholder (‘?’) arguments. The NamedParameterJdbcTemplate class wraps a JdbcTemplate, and delegates to the wrapped JdbcTemplate to do much of its work. An SqlParameterSource is a source of named parameter values to a NamedParameterJdbcTemplate. The BeanPropertySqlParameterSource class is an implementation of SqlParameterSource interface. This class wraps an arbitrary JavaBean (that is, an instance of a class that adheres to the JavaBean conventions), and uses the properties of the wrapped JavaBean as the source of named parameter values.

For this tutorial we will create a standalone maven project in Eclipse. If you already have an idea on how to create a maven project in Eclipse will be great otherwise I will tell you here how to create a maven project in Eclipse.

The following configurations are required in order to run the application
Eclipse Kepler
JDK 1.8
Have maven installed and configured
Spring and MySQL dependencies in pom.xml
Now we will see the below steps how to create a maven based spring project in Eclipse
Step 1. Create a standalone maven project in Eclipse

Go to File -> New -> Other. On popup window under Maven select Maven Project. Then click on Next. Select the workspace location – either default or browse the location. Click on Next. Now in next window select the row as highlighted from the below list of archtypes and click on Next button.

Now enter the required fields (Group Id, Artifact Id) as shown below
Group Id : com.roytuts
Artifact Id : spring-core
Step 2. Modify the pom.xml file as shown below.

<project xmlns="" xmlns:xsi=""
        <!-- mysql driver -->
        <!-- junit -->

Step 3. If you see JRE System Library[J2SE-1.4] then change the version by below process

Do right-click on the project and go to Build -> Configure build path, under Libraries tab click on JRE System Library[J2SE-1.4], click on Edit button and select the appropriate jdk 1.8 from the next window. Click on Finish then Ok.

Step 4. Create src/main/resources folder for putting the resource files.

Do right-click on the project and go New -> Source Folder. Give Folder name: as src/main/resources and click on Finish button.

Step 5. Create an XML file under src/main/resources.

Do right-click on src/main/resources in the project and go New -> file. Give File name: as and click on Finish button.


Step 6. Create spring-datasource.xml file under src/main/resources with the below content.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
    xmlns:xsi="" xmlns:p=""
    xmlns:aop="" xmlns:context=""
    xmlns:jee="" xmlns:tx=""
    <context:annotation-config />
    <context:property-placeholder location="" />
    <bean id="dataSource"
        <property name="driverClassName" value="${JDBC.DRIVER}" />
        <property name="url" value="${JDBC.URL}" />
        <property name="username" value="${JDBC.USERNAME}" />
        <property name="password" value="${JDBC.PASSWORD}" />
    <bean id="namedParameterJdbcTemplate"
        <constructor-arg ref="dataSource" />
    <bean id="cdDao" class="com.roytuts.dao.impl.CdDaoImpl" />

Step 7. Create a MySQL table

USE `cdcol`;
/*Table structure for table `cds` */
  `title` varchar(200) COLLATE latin1_general_ci DEFAULT NULL,
  `interpret` varchar(200) COLLATE latin1_general_ci DEFAULT NULL,
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  PRIMARY KEY (`id`)

Step 8. Create a POJO class for cds.

package com.roytuts.model;
public class Cd {
    private long id;
    private String title;
    private String interpret;
    public Cd() {
    public Cd(String title, String interpret) {
        this.title = title;
        this.interpret = interpret;
    public Cd(long id, String title, String interpret) { = id;
        this.title = title;
        this.interpret = interpret;
    public long getId() {
        return id;
    public void setId(long id) { = id;
    public String getTitle() {
        return title;
    public void setTitle(String title) {
        this.title = title;
    public String getInterpret() {
        return interpret;
    public void setInterpret(String interpret) {
        this.interpret = interpret;

Step 9. Create a DAO

package com.roytuts.dao;
import com.roytuts.model.Cd;
public interface CdDao {
    void addCdNamedParameterJdbcTemplate(Cd cd);
    int countOfCds(Cd cd);

Step 10. Now create a DAO implementation class

package com.roytuts.dao.impl;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import com.roytuts.dao.CdDao;
import com.roytuts.model.Cd;
public class CdDaoImpl implements CdDao {
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    private final String SQL_INSERT_CD_NAMED_PARAMETER = "insert into cds(title, interpret) values(:title, :interpret)";
    public void addCdNamedParameterJdbcTemplate(final Cd cd) {
        // notice how the named parameters match the properties of the 'Cd'
        // class
        SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(
    public int countOfCds(Cd cd) {
        // notice how the named parameters match the properties of the 'Cd'
        // class
        String sql = "select count(*) from cds where title = :title and interpret = :interpret";
        SqlParameterSource namedParameters = new BeanPropertySqlParameterSource(
        return this.namedParameterJdbcTemplate.queryForObject(sql,
                namedParameters, Integer.class);

Step 11. Create JUnit test case under src/test/java folder.

package com.roytuts.dao.impl.test;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import com.roytuts.dao.CdDao;
import com.roytuts.model.Cd;
public class CdDaoImplTest {
    private CdDao cdDao;
    private ApplicationContext applicationContext;
    public void setUp() throws Exception {
        applicationContext = new ClassPathXmlApplicationContext(
        cdDao = applicationContext.getBean("cdDao", CdDao.class);
    public void tearDown() throws Exception {
    public void testAddCdNamedParameterJdbcTemplate() {
        Cd cd = new Cd("Songs", "Soumitra");
    public void testCountOfCds() {
        Cd cd = new Cd("Songs", "Soumitra");
        int count = cdDao.countOfCds(cd);

Step 12. Run the above JUnit test case.

Step 13. Now Look at the database table. You will see row value is inserted. Look into the console where the count is displayed.

Thanks for reading.


Leave a Reply

Your email address will not be published. Required fields are marked *