Home Tutorial Spring Spring3 Jdbc Spring SimpleJdbcTemplate update

 
 

Spring SimpleJdbcTemplate update
Posted on: August 26, 2010 at 12:00 AM
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 .

EXAMPLE

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

UpdateSJT.java

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();
	emp.setId(1101);
	emp.setFirstName("Rakesh");
	emp.setLastName("Chaudhary");
	return emp;
	}
}

UpdateSJTMain.html

package net.roseindia;

import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

public class UpdateSJTMain {

	public static void main(String[] args) {

		XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
				"UpdateSJT.xml"));

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

		myBean.insert(myBean.newEmp());

	}
}

UpdateSJT.xml

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="BSJT" class="net.roseindia.UpdateSJT">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://192.168.10.13:3306/ankdb" />
<property name="user" value="root" />
<property name="password" value="root" />
</bean>
</beans>

Emp.java

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() {
		return this.id;
	}

	public void setId(int id) {
		this.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;
	}

}

OUTPUT

Before executing query database table :

After executing database table :

Download Source Code

Related Tags for Spring SimpleJdbcTemplate update :


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.