Home Tutorial Spring Spring3 Jdbc SimpleJdbcInsert example

 
 

Share on Google+Share on Google+
SimpleJdbcInsert example
Posted on: September 23, 2010 at 12:00 AM
Advertisement
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.

SJInsert.java

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)
			.withTableName("records");
	}
	
	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());
	insertRecord.execute(parameters);
	}
	
	public Emp newEmp() {
	Emp emp = new Emp();
	emp.setId(1101);
	emp.setFirstName("Ankit");
	emp.setLastName("Kaushal");
	return emp;
	}

}
SJInsertMain.java
package net.roseindia;

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

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

SJInsert.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="SJI" class="net.roseindia.SJInsert">
<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> 

Output :

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

Download Source Code

Advertisement

Related Tags for SimpleJdbcInsert example:


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.

Posted on: September 23, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com