Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

NamedParameter


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.Kreationnext;
  2. public class Employee {
  3. private int id;
  4. private String name;
  5. private float salary;
  6. //no-arg and parameterized constructors
  7. //getters and setters
  8. }

EmployeeDao.java

It contains on property jdbcTemplate and one method save.

  1. package com.Kreationnext;
  2. import java.sql.PreparedStatement;
  3. import java.sql.SQLException;
  4. import org.springframework.dao.DataAccessException;
  5. import org.springframework.jdbc.core.PreparedStatementCallback;
  6. import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
  7. import java.util.*;
  8. public class EmpDao {
  9. NamedParameterJdbcTemplate template;
  10. public EmpDao(NamedParameterJdbcTemplate template) {
  11.         this.template = template;
  12. }
  13. public  void save (Emp e){
  14. String query=“insert into employee values (:id,:name,:salary)”;
  15. Map<String,Object> map=new HashMap<String,Object>();
  16. map.put(“id”,e.getId());
  17. map.put(“name”,e.getName());
  18. map.put(“salary”,e.getSalary());
  19. template.execute(query,map,new PreparedStatementCallback() {
  20.     @Override
  21.     public Object doInPreparedStatement(PreparedStatement ps)
  22.             throws SQLException, DataAccessException {
  23.         return ps.executeUpdate();
  24.     }
  25. });
  26. }
  27. }

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. <bean id=“ds” class=“org.springframework.jdbc.datasource.DriverManagerDataSource”>
  9. <property name=“driverClassName” value=“oracle.jdbc.driver.OracleDriver” />
  10. <property name=“url” value=“jdbc:oracle:thin:@localhost:1521:xe” />
  11. <property name=“username” value=“system” />
  12. <property name=“password” value=“oracle” />
  13. </bean>
  14. <bean id=“jtemplate”
  15.  class=“org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate”>
  16. <constructor-arg ref=“ds”></constructor-arg>
  17. </bean>
  18. <bean id=“edao” class=“com.Kreationnext.EmpDao”>
  19. <constructor-arg>
  20. <ref bean=“jtemplate”/>
  21. </constructor-arg>
  22. </bean>
  23. </beans>

SimpleTest.java

This class gets the bean from the applicationContext.xml file and calls the save method.

  1. package com.Kreationnext;
  2. import org.springframework.beans.factory.BeanFactory;
  3. import org.springframework.beans.factory.xml.XmlBeanFactory;
  4. import org.springframework.core.io.ClassPathResource;
  5. import org.springframework.core.io.Resource;
  6. public class SimpleTest {
  7.   public static void main(String[] args) {
  8.     Resource r=new ClassPathResource(“applicationContext.xml”);
  9.     BeanFactory factory=new XmlBeanFactory(r);
  10.     EmpDao dao=(EmpDao)factory.getBean(“edao”);
  11.     dao.save(new Emp(23,“sonoo”,50000));
  12.   }
  13. }

Comments

comments