W3Schools Learner's Blog

W3Schools Programming knowledge summary website

div

12/18/2017

Spring NamedParameterJdbcTemplate Example

Spring provides another way to insert data by named parameter. In such way, we use names instead of ?(question mark). So it is better to remember the data for the column.

Simple example of named parameter query

  1. insert into employee values (:id,:name,:salary)  

Method of NamedParameterJdbcTemplate class

In this example,we are going to call only the execute method of NamedParameterJdbcTemplate class. Syntax of the method is as follows:
  1. pubic T execute(String sql,Map map,PreparedStatementCallback psc)  

Example of NamedParameterJdbcTemplate class

We are assuming that you have created the following table inside the Oracle10g database.
  1. create table employee(  
  2. id number(10),  
  3. name varchar2(100),  
  4. salary number(10)  
  5. );  
Employee.java
This class contains 3 properties with constructors and setter and getters.
  1. package com.javatpoint;  
  2.   
  3. public class Employee {  
  4. private int id;  
  5. private String name;  
  6. private float salary;  
  7. //no-arg and parameterized constructors  
  8. //getters and setters  
  9. }  

EmployeeDao.java
It contains on property jdbcTemplate and one method save.
  1. package com.javatpoint;  
  2.   
  3. import java.sql.PreparedStatement;  
  4. import java.sql.SQLException;  
  5. import org.springframework.dao.DataAccessException;  
  6. import org.springframework.jdbc.core.PreparedStatementCallback;  
  7. import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;  
  8. import java.util.*;  
  9.   
  10. public class EmpDao {  
  11. NamedParameterJdbcTemplate template;  
  12.   
  13. public EmpDao(NamedParameterJdbcTemplate template) {  
  14.         this.template = template;  
  15. }  
  16. public  void save (Emp e){  
  17. String query="insert into employee values (:id,:name,:salary)";  
  18.   
  19. Map<String,Object> map=new HashMap<String,Object>();  
  20. map.put("id",e.getId());  
  21. map.put("name",e.getName());  
  22. map.put("salary",e.getSalary());  
  23.   
  24. template.execute(query,map,new PreparedStatementCallback() {  
  25.     @Override  
  26.     public Object doInPreparedStatement(PreparedStatement ps)  
  27.             throws SQLException, DataAccessException {  
  28.         return ps.executeUpdate();  
  29.     }  
  30. });  
  31. }  
  32. }  
applicationContext.xml
The DriverManagerDataSource is used to contain the information about the database such as driver class name, connnection URL, username and password.

There are a property named datasource in the NamedParameterJdbcTemplate class of DriverManagerDataSource type. So, we need to provide the reference of DriverManagerDataSource object in the NamedParameterJdbcTemplate class for the datasource property.

Here, we are using the NamedParameterJdbcTemplate object in the EmployeeDao class, so we are passing it by the constructor but you can use setter method also.
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans  
  3.     xmlns="http://www.springframework.org/schema/beans"  
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  5.     xmlns:p="http://www.springframework.org/schema/p"  
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
  8.   
  9. <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
  10. <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />  
  11. <property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />  
  12. <property name="username" value="root" />  
  13. <property name="password" value="root" />  
  14. </bean>  
  15.   
  16. <bean id="jtemplate"   
  17.  class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">  
  18. <constructor-arg ref="ds"></constructor-arg>  
  19. </bean>  
  20.   
  21. <bean id="edao" class="com.javatpoint.EmpDao">  
  22. <constructor-arg>  
  23. <ref bean="jtemplate"/>  
  24. </constructor-arg>  
  25. </bean>  
  26.   
  27. </beans>  
SimpleTest.java
This class gets the bean from the applicationContext.xml file and calls the save method.
  1. package com.javatpoint;  
  2.   
  3. import org.springframework.beans.factory.BeanFactory;  
  4. import org.springframework.beans.factory.xml.XmlBeanFactory;  
  5. import org.springframework.core.io.ClassPathResource;  
  6. import org.springframework.core.io.Resource;  
  7.   
  8. public class SimpleTest {  
  9.   public static void main(String[] args) {  
  10.       
  11.     Resource r=new ClassPathResource("applicationContext.xml");  
  12.     BeanFactory factory=new XmlBeanFactory(r);  
  13.       
  14.     EmpDao dao=(EmpDao)factory.getBean("edao");  
  15.     dao.save(new Emp(23,"sonoo",50000));  
  16.       
  17.   }  
  18. }

No comments:

Post a Comment

Note: only a member of this blog may post a comment.