Share on Google+Share on Google+

Spring SimpleJdbcTemplate update

This section is about update method using SimpleJdbcTemplate to update records .

Spring SimpleJdbcTemplate update

This section is about update method using SimpleJdbcTemplate  to update records . The update method is beneficial where you need to issue single update .It is easy and effective in this condition. This method throw DataAccessException .


In this example , we are going to update the database table row using update method . We are using "Emp" class for setting the value.

package net.roseindia;

import javax.sql.DataSource;

import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

public class UpdateSJT {
	private SimpleJdbcTemplate simpleJdbcTemplate;
	private static final String update = 
	"update skills set first_name = ?, last_name = ? where id = ?";

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

	public void insert(Emp emp) {
		simpleJdbcTemplate.update(update, emp.getFirstName(),
				emp.getLastName(), emp.getId());

	public Emp newEmp() {
    Emp emp = new Emp();
	return emp;


package net.roseindia;

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

public class UpdateSJTMain {

	public static void main(String[] args) {

		XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(

		UpdateSJT myBean = (UpdateSJT) beanFactory.getBean("BSJT");




<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns=""
xmlns:xsi="" xmlns:context=""
<bean id="BSJT" class="net.roseindia.UpdateSJT">
<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" />

package net.roseindia;

public class Emp {
	private int id;
	private String firstName;
	private String lastName;

	public String getFirstName() {
		return this.firstName;

	public String getLastName() {
		return this.lastName;

	public int getId() {

	public void setId(int id) { = id;

	// setters methods

	public void setFirstName(String firstName) {
		this.firstName = firstName;

	public void setLastName(String lastName) {
		this.lastName = lastName;

	public String toString() {
		return id + " " + firstName + " " + lastName;



Before executing query database table :

After executing 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.