Home Tutorial Spring Spring3 Jdbc Spring SimpleJdbcTemplate example

 
 

Share on Google+Share on Google+
Spring SimpleJdbcTemplate example
Posted on: August 25, 2010 at 12:00 AM
Advertisement
This section is about SimpleJdbcTemplate and it's use in database handling in Spring.

Spring SimpleJdbcTemplate example

The SimpleJdbcTemplate has all the features of old JdbcTemplate and also support some features of  Java 5 i.e varargs and autoboxing. It best suited when you need not to access all the feature of JdbcTemplate. It has a simpler API and basically construct to support java 5.Thats why it has more method to exploit varargs.

The getJdbcOperations() method is used to access those methods which are defined in JdbcTemplate. You have to call these method on SimpleJdbcTemplate  . The main drawback is that you need to cast these methods as the methods on JdbcOperations interface are not genric.

EXAMPLE

In this example , we are fetching record using SimpleJdbcTemplate. We are using row mapper to map  current row. It is using "Emp" class object to set and get the current row record.

 SimpleJTemplate.java

package net.roseindia;

import java.sql.ResultSet;
import java.sql.SQLException;

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

public class SimpleJTemplate {
	private SimpleJdbcTemplate simpleJdbcTemplate;

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

	public Emp findEmp(String specialty, int age) {
	    String sql = "select id, first_name, last_name from skills"
				+ " where specialty = ? and age = ?";
		RowMapper mapper = new RowMapper() {
		public Emp mapRow(ResultSet rs, int rowNum) throws SQLException {
		Emp emp = new Emp();
		emp.setId(rs.getLong("id"));
		emp.setFirstName(rs.getString("first_name"));
		emp.setLastName(rs.getString("last_name"));
		System.out.print(rs.getLong("id") + " ");
		System.out.print(rs.getString("first_name") + " ");
		System.out.print(rs.getString("last_name"));
		return emp;
			}
		};

		return this.simpleJdbcTemplate.queryForObject(sql, mapper, specialty,
				age);
	}
}

SimpleJTemplate.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="SJT" class="net.roseindia.SimpleJTemplate">
<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>

SimpleJTemplateMain.java

package net.roseindia;

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

public class SimpleJTemplateMain {

	public static void main(String[] args) {

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

	SimpleJTemplate myBean = (SimpleJTemplate) beanFactory.getBean("SJT");
	myBean.findEmp("Spring", 25);

	}
}

OUTPUT

Database table 'skills' :

 Eclipse console after code execution :

Download Source Code

Advertisement

Related Tags for Spring SimpleJdbcTemplate 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: August 25, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com