Share on Google+Share on Google+

Spring SimpleJdbcTemplate batchUpdate

This section is about batchUpdate method for issuing multiple SQL updates using SimpleJdbcTemplate.

Spring SimpleJdbcTemplate batchUpdate

The performance of Jdbc driver improves when same prepared statement is using for batch of multiple calls. In simplejdbctemplate, you don't need to implement special batch interface(like in JdbcTemplate). You just need to pass all the parameters value in the same call. It has a internal  prepared statement setter which iterate over these value. The batchUpdate method  issues multiple SQL updates on a single Statement.


In this example we are using "?" placeholder inside SQL statement. For this, the updated values should be pass in a list containing an object array. For each placeholder, the object array must have one entry , The order must be the same as it's order in SQL query or statement.

package net.roseindia;

import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

public class BatchUpdateSJT {
	private SimpleJdbcTemplate simpleJdbcTemplate;

	public void setDataSource(DataSource dataSource) {
		this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);

	public int[] insertRows() {
	List batchArgs = new ArrayList();
	batchArgs.add(new Object[] { "A", "K", 1101 });
	batchArgs.add(new Object[] { "B", "K", 1102 });
	batchArgs.add(new Object[] { "C", "K", 1103 });
	batchArgs.add(new Object[] { "D", "K", 1104 });
	batchArgs.add(new Object[] { "E", "K", 1105 });
	int[] updateCounts = simpleJdbcTemplate.batchUpdate(
		"update skills set first_name = ?, last_name = ? where id = ?",
	return updateCounts;

package net.roseindia;

import org.springframework.beans.factory.xml.XmlBeanFactory;

public class BatchUpdateSJTMain {

	public static void main(String[] args) {

	XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(

	BatchUpdateSJT myBean = (BatchUpdateSJT) beanFactory.getBean("BUSJT");




<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi="" xmlns:context=""
<bean id="BUSJT" class="net.roseindia.BatchUpdateSJT">
<property name="dataSource" ref="dataSource" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://" />
<property name="user" value="root" />
<property name="password" value="root" />


Before code execution, database table:

After Code execution , database table :

Download Source Code


Posted on: August 26, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.