Share on Google+Share on Google+

Spring SimpleJdbcTemplate example

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.


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.

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();
		System.out.print(rs.getLong("id") + " ");
		System.out.print(rs.getString("first_name") + " ");
		return emp;

		return this.simpleJdbcTemplate.queryForObject(sql, mapper, specialty,


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

package net.roseindia;

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

public class SimpleJTemplateMain {

	public static void main(String[] args) {

	XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource(

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



Database table 'skills' :

 Eclipse console after code execution :

Download Source Code


Posted on: August 25, 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.