Share on Google+Share on Google+

Spring NamedParameterJdbcTemplate example

This section contains the NamedParameterJdbcTemplate description with an example.

Spring NamedParameterJdbcTemplate example

The NamedParameterJdbcTemplate provide better approach than JdbcTemplate ,where multiple parameters are in use for an SQL statement. It eliminated need of traditional JDBC "?" and provide named parameters. It is easy to use and provide better documentation. It functionality is similar to JdbcTemplate except it incorporate named parameters instead of "?" placeholder.


package net.roseindia;

import javax.sql.DataSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

public class NPJTemplate {

	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

	public void setDataSource(DataSource dataSource) {
		this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(

	public int CountStundentBranch(String Branch) {
	String sql = "select count(*) from studentrecord where branch = :branch";
	SqlParameterSource namedParameters = new MapSqlParameterSource(
				"branch", Branch);
	return namedParameterJdbcTemplate.queryForInt(sql, namedParameters);

package net.roseindia;

import org.springframework.beans.factory.xml.XmlBeanFactory;

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

    XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(

    NPJTemplate myBean = (NPJTemplate) beanFactory.getBean("NPJT");

			+ myBean.CountStundentBranch("Information Technology"));



<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi="" xmlns:context=""
<bean id="NPJT" class="net.roseindia.NPJTemplate">
<property name="dataSource" ref="dataSource" />
<bean id="designationDAO" class="net.roseindia.DesignationQuery">
<property name="dataSource" ref="dataSource" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://" />
<property name="user" value="root" />
<property name="password" value="root" />

Description of the program :

For using NamedParameterJdbcTemplate ,you need to import following classes : . . .

Take a look on the following code to understand the working of NamedParameterJdbcTemplate :

1.public int CountStundentBranch(String Branch) {
2.   String sql = "select count(*) from studentrecord where branch = :branch";
3.   SqlParameterSource namedParameters = new MapSqlParameterSource(
	 "branch", Branch);
4.  return namedParameterJdbcTemplate.queryForInt(sql, namedParameters);
5. }

In query , we are using named parameter instead of  "?" placeholder. The SqlParameterSource object is used to map the value passed by the function to named parameter. In this case name parameter is "branch"  and passed value is stored in "Branch" .After this namedParameterJdbcTemplate pass the query and SqlparameterSource object to execute it.


The database table :

After executing code eclipse console :

Download Source Code


Posted on: August 23, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.