Home arrow Java & J2EE arrow Page 2 - Data Access Using Spring Framework JDBC

Using JdbcTemplate Step-by-Step - Java

Persisting and accessing data forms one of the most routine yet core functionalities of any application. In the world of JEE, there are many APIs as well as frameworks to achieve to achieve the same. The Spring Framework is no exception. This article will explain how to use this framework for persisting and accessing data in your applications.

  1. Data Access Using Spring Framework JDBC
  2. Using JdbcTemplate Step-by-Step
  3. Configuring the Bean
  4. JdbcTemplate in the Real World
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 17
December 16, 2008

print this article



Next let us see how to use Spring's JDBC support. Since JdbcTemplate is the 'lowest-level' of all the types, the steps required for JdbcTemplate become part of the steps for all other types. The following are the steps required to make use of JdbcTemplate:

1. Develop the bean

2. Configure the bean and DataSource

3. Develop the client

Of these, the second and third steps can be divided into sub-steps. Here are the details.

Develop the bean: The bean or POJO is similar to any other POJO used with the Spring Framework except for one difference. The POJO developed to be used with JdbcTemplate requires a setter for the DataSource object. The following is an example of a POJO that can be used with JdbcTemplate. The setter will pass the DataSource object to the instance of JdbcTemplate.


public class JdbcEventDao


private DataSource dataSource;

public void setDataSource(DataSource dataSource)


this.dataSource = dataSource;



public DataSource getDataSource()


return dataSource;



The POJO can contain other methods that can work with the DataSource.

Configure the DataSource and the bean: The configuration is done using the XML file. Lets call it beans.xml. This step can be further divided into configuring the DataSource, and configuring the bean. We will look at the DataSource first. 


A DataSource is configured by declaring it as a bean and providing the required information as the child nodes of the bean declaration. The configuration is done as follows:

First, a bean is declared whose class is mapped to an implementation of
DataSource. One of the commonly-used implementations is org.apache.commons.dbcp.BasicDataSource. For example, to declare a bean with its class mapped to BasicDataSource class as the DataSource implementation class, the statement will be

<bean id="dataSource" destroy-method="close"





Second, the required details for the DataSource such as driver name, URL of the DataSource, credentials etc. can be passed onto the DataSource through property tags. The property tags are the children of the <bean> tag. In versions prior to 2.5, the property tags had the <value> tag as the child tag. For example, to pass "com.mysql.jdbc.Driver" as the value of a property named "driverClassName," the statement in version 2.5 and above will be

<bean id="dataSource" destroy-method="close"

<property name="driverClassName"


and in previous versions it will be

<bean id="dataSource" destroy-method="close"

<property name="driverClassName">

<value> com.mysql.jdbc.Driver</value>



The next step is configuring the bean.

>>> More Java & J2EE Articles          >>> More By A.P.Rajshekhar

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates


Dev Shed Tutorial Topics: