Home Tutorial Spring Spring3 Jdbc SimpleJdbcInsert, Retrieving auto-generated keys

 
 

Share on Google+Share on Google+
SimpleJdbcInsert, Retrieving auto-generated keys
Posted on: September 23, 2010 at 12:00 AM
Advertisement
This section is about retrieving auto-generated keys using usingGeneratedKeyColumns" method of SimpleJdbcInsert.

SimpleJdbcInsert, Retrieving auto-generated keys

The auto-generated keys can be generated using "usingGeneratedKeyColumns" method. The name of the auto generated key column can be specified by passing it's name to method "usingGeneratedKeyColumns". The column must be integer or long and also it must be set for auto increment in database.

Example :

The main difference between simple insert and insert using "usingGeneratedKeyColumns" method is that you need not to add the 'id' to the Map. The "executeAndReturnKey" method returns a 'java.lang.Number' object whose numerical instance can be used in our domain class.

SJIautokey.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 SJIautokey {
	private SimpleJdbcInsert insertRecord;

	public void setDataSource(DataSource dataSource) {
	new SimpleJdbcTemplate(dataSource);
	this.insertRecord = new SimpleJdbcInsert(dataSource).withTableName(
			"records").usingGeneratedKeyColumns("id");
	}

	public void add(Emp e) {
	Map parameters = new HashMap(3);
	parameters.put("firstname", e.getFirstName());
	parameters.put("lastname", e.getLastName());
	Number newId = insertRecord.executeAndReturnKey(parameters);
	e.setId(newId.intValue());
	}

	public Emp newEmp() {
	Emp emp = new Emp();
	emp.setFirstName("Mahima");
	emp.setLastName("Singhla");
	return emp;
	}
}

SJIautokeyMain.java

package net.roseindia;

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

public class SJIautokeyMain {

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

SJIautokey.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="SJIautokey" class="net.roseindia.SJIautokey">
<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 code , the rows affected at database table "records" :

Download Source Code

Advertisement

Related Tags for SimpleJdbcInsert, Retrieving auto-generated keys:


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