Example of PreparedStatement in Spring JdbcTemplate

In this page We can learn execute parameterized query using Spring JdbcTemplate by the help of execute() method of JdbcTemplate class. To use parameterized query, we pass the instance of PreparedStatementCallback in the execute method.

Syntax of execute method to use parameterized query

public T execute(String sql,PreparedStatementCallback<T>);

PreparedStatementCallback interface

It processes the input parameters and output results. In such case, you don't need to care about single and double quotes.

Method of PreparedStatementCallback interface

It has only one method doInPreparedStatement. Syntax of the method is given below:

public T doInPreparedStatement(PreparedStatement ps)throws SQLException, DataAccessException

Example of using PreparedStatement in Spring

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 jdbcTemplate and one method saveEmployeeByPreparedStatement. You must understand the concept of annonymous class to understand the code of the method.

package com.javatportal;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;

public class EmployeeDao {
private JdbcTemplate jdbcTemplate;

public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
	this.jdbcTemplate = jdbcTemplate;
}

public Boolean saveEmployeeByPreparedStatement(final Employee e){
	String query="insert into employee values(?,?,?)";
	return jdbcTemplate.execute(query,new PreparedStatementCallback<Boolean>(){
	@Override
	public Boolean doInPreparedStatement(PreparedStatement ps)
			throws SQLException, DataAccessException {
			
		ps.setInt(1,e.getId());
		ps.setString(2,e.getName());
		ps.setFloat(3,e.getSalary());
			
		return ps.execute();
			
	}
	});
}


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

Here, we are using the JdbcTemplate object in the EmployeeDao class, so we are passing it by the setter method but you can use constructor 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="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>

<bean id="edao" class="com.javatportal.EmployeeDao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>


</beans>
Test.java

This class gets the bean from the applicationContext.xml file and calls the saveEmployeeByPreparedStatement() method.

package com.javatportal;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {

public static void main(String[] args) {
	ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
		
	EmployeeDao dao=(EmployeeDao)ctx.getBean("edao");
	dao.saveEmployeeByPreparedStatement(new Employee(108,"Amit",35000));
}
}