Share on Google+Share on Google+

SimpleJdbcInsert example

This section is about inserting data into database table using SimpleJdbcInsert.

SimpleJdbcInsert example

This approach minimize the database metadata. This optimization is to bound the amount of necessary configuration. In this approach you need to provide name of table and a map of parameters matching the column name. This works only if database provide sufficient metadata. In case it is not provided adequately, then you have to provide explicit configuration of the parameters.

Using database metadata, SimpleJdbcInsert provide a easy configuration. The database metadata can be fetched via JDBC driver. It means SimpleJdbcInsert provide minimum configuration options. The SimpleJdbcInsert must be initialize inside setDataSource method and table name can be set using "withTableName" method.

Example :

In this example, we are going to insert a row in database table using SimpleJdbcInsert . For this we need to provide name of table and a map of parameters matching the column name.

Here, only parameter taken by 'execute' method is 'java.utils.Map'. The name of the keys used for the Map, must be same as the name of the database column name.

package net.roseindia;

import java.util.HashMap;
import java.util.Map;

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

public class SJInsert {

	private SimpleJdbcInsert insertRecord;
	public void setDataSource(DataSource dataSource) {
	new SimpleJdbcTemplate(dataSource);
	this.insertRecord = new SimpleJdbcInsert(dataSource)
	public void add(Emp e) {
	Map parameters = new HashMap(3);
	parameters.put("id", e.getId());
	parameters.put("firstname", e.getFirstName());
	parameters.put("lastname", e.getLastName());
	public Emp newEmp() {
	Emp emp = new Emp();
	return emp;

package net.roseindia;

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

public class SJInsertMain {
	public static void main(String[] args) {
	XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(
	SJInsert myBean = (SJInsert) beanFactory.getBean("SJI");


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

Output :

After executing ' ' code , the table 'records' have following data :

Download Source Code



Posted on: September 23, 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.


Advertisement null