English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Spring NamedParameterJdbcTemplate example

Spring provides another method for inserting data through named parameters. In this way, we use names instead of ? (question mark). Therefore, it is best to remember the data of the column.

A simple example of named parameter query

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

Methods of the NamedParameterJdbcTemplate class

In this example, we will only call the execute method of the NamedParameterJdbcTemplate class. The syntax of the method is as follows:

public T execute(String sql, Map map, PreparedStatementCallback psc)

An example of the NamedParameterJdbcTemplate class

We assume that you have already installed Oracle10The following table was created in the g database.

create table employee(
id number(10)
name varchar2(100),
salary number(10)
);

Employee.java

This class includes3a class with a constructor, setter, and getter properties.

package com.w3codebox;
public class Employee {
private int id;
private String name;
private float salary;
//no-arg and parameterized constructors
//getters and setters
}

EmployeeDao.java

It contains a property jdbcTemplate and a method save.

package com.w3codebox;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import java.util.*;
public class EmpDao {
NamedParameterJdbcTemplate template;
public EmpDao(NamedParameterJdbcTemplate template) {
        this.template = template;
}
public  void save (Emp e){
String query="insert into employee values (:id,:name,:salary)";
Map<String,Object> map=new HashMap<String,Object>();
map.put("id",e.getId());
map.put("name",e.getName());
map.put("salary",e.getSalary());
template.execute(query,map,new PreparedStatementCallback() {
    @Override
    public Object doInPreparedStatement(PreparedStatement ps)
            throws SQLException, DataAccessException {
        return ps.executeUpdate();
    }
});
}
}

applicationContext.xml

DriverManagerDataSource used to contain information about the database, such as the driver class name, connection URL, username, and password.

In the NamedParameterJdbcTemplate class of type DriverManagerDataSource, there is a named datasource The attribute. Therefore, we need to provide a reference to the DriverManagerDataSource object for the data source attribute in the NamedParameterJdbcTemplate class.

Here, we use the NamedParameterJdbcTemplate object in the EmployeeDao class, so we pass it through the constructor, but you can also use the setter method.

<?xml version="1.0" encoding="UTF-8"?>
<beans
    xmlns="http://www.springframework.org/schema/beans
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
    xmlns:p="http://www.springframework.org/schema/p
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@localhost:1521:xe" />
<property name="username" value="system" />
<property name="password" value="oracle" />
</bean>
<bean id="jtemplate" 
 class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
<constructor-arg ref="ds"></constructor-arg>
</bean>
<bean id="edao" class="com.w3codebox.EmpDao">
<constructor-arg>
<ref bean="jtemplate"/>
</constructor-arg>
</bean>
</beans>

SimpleTest.java

This class retrieves a Bean from the applicationContext.xml file and calls the save method.

package com.w3codebox;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
public class SimpleTest {
  public static void main(String[] args) {
    Resource r=new ClassPathResource("applicationContext.xml");
    BeanFactory factory=new XmlBeanFactory(r);
    EmpDao dao=(EmpDao)factory.getBean("edao");
    dao.save(new Emp(23,"sonoo",50000));
  }
}