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

JdbcTemplate in the Real World - 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



The application being developed will perform two functions. First, it will retrieve data related to a given ID. Second, it will delete the data related to a given ID. It is a simple application, but it will give you an idea of how JDBC and the Spring Framework work together. The application will have three main files:

  1. JdbcDataSource.java - The POJO having getter and setter for the DataSource reference.
  2. beans.xml - The configuration file for the Spring Framework.
  3. JdbcClient.java - A client that makes use of JdbcTemplate for retrieval and deletion functionalities.

Let us start with JdbcDataSource.java. It is same as the JdbcEventDao class. The following is the code:

public class JdbcDataSource


private DataSource dataSource;

public void setDataSource(DataSource dataSource)


this.dataSource = dataSource;



public DataSource getDataSource()


return dataSource;



Next comes the beans.xml. It contains the DataSource configuration as well as the bean configuration.

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


<property name="driverClassName">

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


<property name="url">



<property name="username">



<property name="password">




<bean id="dao" class="JdbcDataSource">

<property name="dataSource">

<ref local="dataSource"/>



And at last we've reached the client. It takes command line arguments and, according to the first argument, either retrieves or deletes using the second argument as the ID. Here is the code.

import java.io.*;

import org.springframework.beans.factory.*;

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

import org.springframework.core.io.*;

import org.springframework.jdbc.core.*;

import org.springframework.jdbc.datasource.*;

import org.springframework.jdbc.object.*;

import org.springframework.jdbc.support.*;

public class JdbcClient


public static void main(String args[]) throws Exception




Resource res = new ClassPathResource("beans.xml");

BeanFactory factory = new XmlBeanFactory(res);

JdbcDataSource bean = (JdbcDataSource)factory.getbean("dao");

JdbcTemplate template = new JdbcTemplate(bean.getDataSource());




List list;

list = template.queryForList("select * from indents where"+
" indent_id ='"+args[1]+"'");

Iterator i=list.iterator();



Object ob = i.next();







template.execute("Delete from indents where indent_id ='" +



}catch(Exception e)





The delete functionality makes use of the execute method of the JdbcTemplate class. This method can be used to execute other SQL statements including inserts and updates.

That completes this section as well as brings us to the end of this discussion. The approach discussed here is just one of the many approaches to using the Spring Framework's JDBC support. There are other approaches for the same. Those will be discussed in the future. Till then...

>>> 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: