<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>The <b>NamedParameterJdbcTemplate </b>class helps you specify the named parameters inste<b>a</b>d of classic placeholder(&#8216;?&#8217;) argument. Named parameters improves readability and are easier to maintain.</p>
<div id="ads-id" align="center"></div>
<div style="background-color: #f2f9fc; border: 1px solid #c9e6f2; border-radius: 3px; padding: 16px; line-height: 1.45;"><span style="color: red; font-size: x-large; text-align: center;"><b>Popular Tutorials</b></span></p>
<ul style="text-align: left;">
<li><b><a href="https://dineshonjava.com/spring-tutorial/"><em><strong>Spring Tutorial</strong> </em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-web-mvc-framework-chapter-38/"><strong><em>Spring MVC Web Tutorial </em></strong></a></b></li>
<li><b><a href="https://dineshonjava.com/introduction-to-spring-boot-a-spring-boot-complete-guide/"><strong>Spring Boot Tutorial</strong> </a></b></li>
<li><b><a href="https://dineshonjava.com/spring-security-take-baby-step-to-secure/"><em>Spring Security Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-aop-tutorial-with-example-aspect-advice-pointcut-joinpoint/"><em>Spring AOP Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/"><em>Spring JDBC Tutorial</em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-hateoas-hypermedia-driven-restful-web-service/"><em><strong>Spring HATEOAS </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/microservices-with-spring-boot/"><em><strong>Microservices with Spring Boot</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/jax-rs-web-service-tutorial/"><strong><em>REST Webservice</em> </strong></a></b></li>
<li><b><a href="https://dineshonjava.com/core-java-baby-step-to-be-best-java-ian/"><em><strong>Core Java </strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/hibernate-3-on-baby-steps/"><em><strong>Hibernate Tutorial</strong></em></a></b></li>
<li><b><a href="https://dineshonjava.com/spring-batch-process-with-example/"><strong><em>Spring Batch</em> </strong></a></b></li>
</ul>
</div>
<div class="separator" style="clear: both; text-align: center;"><a href="https://certification-questions.com/buy-mock-exams.html?affiliateCode=4f5a35d8-0022-4c9c-b8d3-265ed0b825a2&;utm_source=Dineshh&;utm_medium=affiliate&;utm_campaign=affiliate" target="_blank" rel="noopener"><img src="https://dineshonjava.com/wp-content/uploads/2012/12/spring-certification-dumps-2.png" border="0" /></a></div>
<p>The <b>NamedParameterJdbcTemplate </b>provide better approach than <b><a href="https://dineshonjava.com/using-jdbctemplate-in-spring-chapter-33/">JdbcTemplate</a> </b>,where multiple parameters are in use for an <b>SQL </b>statement. It eliminated need of traditional JDBC &#8220;?&#8221; and provide named parameters. It is easy to use and provide better documentation. It functionality is similar to <b><a href="https://dineshonjava.com/using-jdbctemplate-in-spring-chapter-33/">JdbcTemplate</a> </b>except it incorporate named parameters instead of &#8220;?&#8221; placeholder.</p>
<p>In <a href="https://dineshonjava.com/using-jdbctemplate-in-spring-chapter-33/"><b>JdbcTemplate</b></a>, SQL parameters are represented by a special placeholder “<b>?</b>” symbol and bind it by position. The problem is whenever the order of parameter is changed, you have to change the parameters bindings as well, it’s error prone and cumbersome to maintain it.</p>
<p> ;</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2012/12/namedjdbctemplate.jpg" width="640" height="144" border="0" /></div>
<p>In additional, the named parameters are only support in <b>SimpleJdbcTemplate</b> and <b>NamedParameterJdbcTemplate</b>.</p>
</div>
<p><b>EmpDao.java</b></p>
<pre class="highlight">package com.dineshonjava.sdnext.dao; 
 
import java.util.List; 
 
import com.dineshonjava.sdnext.domain.Employee; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public interface EmpDao { 
 
 /** 
 * This is the method to be used to create 
 * a record in the Employee table. 
 */ 
 void create(String name, Integer age, Long salary); 
 /** 
 * This is the method to be used to list down 
 * a record from the Employee table corresponding 
 * to a passed Employee id. 
 */ 
 Employee getEmployee(Integer empid); 
 /** 
 * This is the method to be used to list down 
 * all the records from the Employee table. 
 */ 
 List listEmployees(); 
 /** 
 * This is the method to be used to delete 
 * a record from the Employee table corresponding 
 * to a passed Employee id. 
 */ 
 void delete(Integer empid); 
 /** 
 * This is the method to be used to update 
 * a record into the Employee table. 
 */ 
 void update(Integer empid, Integer age); 
} 
</pre>
<p><b>EmployeeDaoImpl.java</b></p>
<pre class="highlight">package com.dineshonjava.sdnext.dao.impl; 
 
import java.util.List; 
 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; 
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; 
import org.springframework.jdbc.core.namedparam.SqlParameterSource; 
import org.springframework.stereotype.Component; 
 
import com.dineshonjava.sdnext.dao.EmpDao; 
import com.dineshonjava.sdnext.domain.Employee; 
import com.dineshonjava.sdnext.jdbc.utils.EmployeeMapper; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
@Component 
public class EmployeeDaoImpl implements EmpDao { 
 @Autowired 
 private NamedParameterJdbcTemplate namedParameterJdbcTemplate; 
 
 /** 
 * @param namedParameterJdbcTemplate the namedParameterJdbcTemplate to set 
 */ 
 public void setNamedParameterJdbcTemplate(NamedParameterJdbcTemplate namedParameterJdbcTemplate) { 
 this.namedParameterJdbcTemplate = namedParameterJdbcTemplate; 
 } 
 
@Override 
public void create(String name, Integer age, Long salary) { 
 String SQL = "INSERT INTO Employee (name, age, salary) VALUES (:name, :age, :salary)"; 
 Map namedParameters = new HashMap(); 
 namedParameters.put("name", name); 
 namedParameters.put("age", age); 
 namedParameters.put("salary", salary); 
 namedParameterJdbcTemplate.update(SQL, namedParameters); 
System.out.println("Created Record Name = " + name + " Age = " + age+ " Salary = " + salary); 
} 
 
@Override 
public Employee getEmployee(Integer empid) { 
 String SQL = "SELECT * FROM Employee WHERE empid = :empid"; 
 SqlParameterSource namedParameters = new MapSqlParameterSource("empid", Integer.valueOf(empid)); 
Employee employee = (Employee) namedParameterJdbcTemplate.queryForObject(SQL, namedParameters, new EmployeeMapper()); 
 return employee; 
} 
 
@Override 
public List listEmployees() { 
 String SQL = "SELECT * FROM Employee"; 
 List employees = (List) namedParameterJdbcTemplate.query(SQL, new EmployeeMapper()); 
 return employees; 
} 
 
@Override 
public void delete(Integer empid) { 
 String SQL = "DELETE FROM Employee WHERE empid = :empid"; 
 SqlParameterSource namedParameters = new MapSqlParameterSource("empid", Integer.valueOf(empid)); 
 namedParameterJdbcTemplate.update(SQL, namedParameters); 
 System.out.println("Deleted Record with EMPID = " + empid ); 
} 
 
@Override 
public void update(Integer empid, Integer age) { 
 String SQL = "UPDATE Employee SET age = :age WHERE empid = :empid"; 
 SqlParameterSource namedParameters = new MapSqlParameterSource(); 
 namedParameters.addValue("age", age); 
 namedParameters.addValue("empid", empid); 
 namedParameterJdbcTemplate.update(SQL, namedParameters); 
 System.out.println("Updated Record with EMPID = " + empid ); 
 } 
} 
</pre>
<p><b>EmployeeMapper.java</b></p>
<pre class="highlight">package com.dineshonjava.sdnext.jdbc.utils; 
 
import java.sql.ResultSet; 
import java.sql.SQLException; 
 
import org.springframework.jdbc.core.RowMapper; 
 
import com.dineshonjava.sdnext.domain.Employee; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class EmployeeMapper implements RowMapper { 
 public Employee mapRow(ResultSet rs, int rowNum) throws SQLException { 
 Employee employee = new Employee(); 
 employee.setEmpid(rs.getInt("empid")); 
 employee.setName(rs.getString("name")); 
 employee.setAge(rs.getInt("age")); 
 employee.setSalary(rs.getLong("salary")); 
 return employee; 
 } 
} 
</pre>
<p><b>Spring.xml</b></p>
<pre class="highlight"><;beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:>
</div>
<div style="background-color: #f2f9fc; border-radius: 3px; border: 1px solid #c9e6f2; line-height: 1.45; padding: 16px;"><b>Spring JDBC Framework Tutorial</b></p>
<ol style="text-align: left;">
<li><b><a href="https://dineshonjava.com/using-spring-jdbc-framework-chapter-32/">Spring JDBC Framework</a></b></li>
<li><b><a href="https://dineshonjava.com/using-jdbctemplate-in-spring-chapter-33/">Using JdbcTemplate in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/implementing-rowmapper-chapter-34/">Implementing RowMapper</a></b></li>
<li><b><a href="https://dineshonjava.com/dao-support-classes-in-spring-chapter-35/">DAO Support Classes in Spring</a></b></li>
<li><b><a href="https://dineshonjava.com/using-namedparameterjdbctemplate-in/">Using NamedParameterJdbcTemplate in Spring with Example</a></b></li>
<li><b><a href="https://dineshonjava.com/spring-simplejdbctemplate-example/">Spring SimpleJdbcTemplate example</a></b></li>
<li><b><a href="https://dineshonjava.com/spring-simplejdbcinsert-example/">Spring SimpleJdbcInsert example</a></b></li>
<li><b><a href="https://dineshonjava.com/stored-procedure-simplejdbccall-in/">Stored Procedure with SimpleJdbcCall in Spring</a></b></li>
</ol>
</div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/dao-support-classes-in-spring-chapter-35/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-simplejdbctemplate-example/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '633'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…