Suggested Pages

Tuesday, June 19, 2012

Spring JdbcTemplate - Tutorial

In the following post i'll show an example of a way to use JdbcTemplate in Spring framework.
JdbcTemplate is a useful class that provides simple methods to retrieve, update and insert entities in the DB.
In the Javadoc it's written: JdbcTemplate semplifies the use of JDBC avoiding the common errors such as forgetting to always close the connection.
The method shown in this post has the following signature: List<T> query(String sql, RowMapper rowMapper). This method executes an SQL query and maps each row of the ResultSet to a Java object via a RowMapper.
RowMapper interface has a method: T mapRow(ResultSet resultSet, int arg1) throws SQLException that must be implemented. It has to map each row of data in the ResultSet into an Entity of the application domain.

As you can see in the following snippet, in the Spring context it's declared a bean called jdbcTemplate ( see ContactDAOTest-context.xml) and then i will use it in my test class ( via injection. You can notice that JdbcTemplate is a bean that only needs a Datasource to communicate with the database.


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<beans xmlns=""
 xmlns:aop="" xmlns:context=""
 xmlns:jee="" xmlns:tx=""
 xmlns:xsi="" xmlns:jaxrs=""

 <!-- annotation support -->
 <context:annotation-config />

 <!-- support for transaction -->
 <tx:annotation-driven />

 <!-- scan package for @Repository annotations -->
 <context:component-scan base-package="com.simonefolinojavablog.persistence.dao" />

 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
  <property name="dataSource" ref="dataSource" />

 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <property name="driverClass" value="com.p6spy.engine.spy.P6SpyDriver" />
  <property name="jdbcUrl" value="jdbc:hsqldb:mem:SN" />
  <property name="user" value="sa" />
  <property name="password" value="" />

 <bean id="entityManagerFactory"
  <property name="persistenceUnitName" value="persistenceUnitTest" />
  <property name="dataSource" ref="dataSource" />
  <property name="jpaVendorAdapter">
   <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    <property name="showSql" value="true" />
    <property name="databasePlatform" value="org.hibernate.dialect.HSQLDialect" />
    <property name="generateDdl" value="true" />

 <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
  <property name="entityManagerFactory" ref="entityManagerFactory" />
  <property name="dataSource" ref="dataSource" />


The following class is a Test Class that makes use of JdbcTemplate.
I use query method of JdbcTemplate which has two parameters:
  • String: the sql statement to retrieve data from the DB;
  • RowMapper: an interface that we'll implement using an Anonymous Class.
In practice we'll make with RowMapper a sort of mapping between the ResultSet and the Contact entity.


import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.simonefolinojavablog.persistence.dao.ContactDAO;
import com.simonefolinojavablog.persistence.entity.Contact;

@ContextConfiguration(value = "ContactDAOTest-context.xml")
public class ContactDAOTest {

 private JdbcTemplate jdbcTemplate;

 private ContactDAO contactDAO;

 private static Contact contact1;
 private static Contact contact2;

 public static void initContact() {
  System.out.println("------------initContact(): start -------");
  contact1 = new Contact();
  contact2 = new Contact();
  System.out.println("------------initContact(): end -------");

 public void insert(){;;
 public void testQuery() {
  List<Contact> contacts = jdbcTemplate.query("select * from Contacts", new RowMapper<Contact>() {
   public Contact mapRow(ResultSet resultSet, int arg1) throws SQLException {
    Contact contact=new Contact();
    return contact;



No comments :

Post a Comment

Suggested Pages