Spring SimpleJdbcTemplate Example

SimpleJdbcTemplate class wraps the JdbcTemplate class and provides the update method where we can pass arbitrary number of arguments.

Syntax of update method of SimpleJdbcTemplate class

int update(String sql,Object... parameters)

We should pass the parameter values in the update method in the order they are defined in the parameterized query.


Example of SimpleJdbcTemplate class

We are assuming that you have created the following table inside the Oracle10g database.

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

This class contains 3 properties with constructors and setter and getters.

package com.javatportal;

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

It contains one property SimpleJdbcTemplate and one method update. In such case, update method will update only name for the corresponding id. If you want to update the name and salary both, comment the above two lines of code of the update method and uncomment the 2 lines of code given below.

package com.javatportal;

import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
public class EmpDao {
SimpleJdbcTemplate template;

public EmpDao(SimpleJdbcTemplate template) {
		this.template = template;
}
public int update (Emp e){
String query="update employee set name=? where id=?";
return template.update(query,e.getName(),e.getId());

//String query="update employee set name=?,salary=? where id=?";
//return template.update(query,e.getName(),e.getSalary(),e.getId());
}

}
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 SimpleJdbcTemplate class of DriverManagerDataSource type. So, we need to provide the reference of DriverManagerDataSource object in the SimpleJdbcTemplate class for the datasource property.

Here, we are using the SimpleJdbcTemplate object in the EmployeeDao class, so we are passing it by the constructor but you can use setter method also.

<?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.simple.SimpleJdbcTemplate">
<constructor-arg ref="ds"></constructor-arg>
</bean>

<bean id="edao" class="com.javatportal.EmpDao">
<constructor-arg>
<ref bean="jtemplate"/>
</constructor-arg>
</bean>

</beans>
SimpleTest.java

This class gets the bean from the applicationContext.xml file and calls the update method of EmpDao class.

package com.javatportal;

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");
	int status=dao.update(new Emp(23,"Tarun",35000));
	System.out.println(status);
}
}