Home arrow Java & J2EE arrow Data Access Using Spring Framework JDBC

Data Access Using Spring Framework JDBC

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 beauty of the Spring Framework is the flexibility it provides. Using the Spring Framework, one can integrate low-level APIs such as JDBC or high-level frameworks such as Hibernate into an application using the same technique - Dependency Injection. In this discussion, the focus will be on using the Spring Framework's data access functionality with JDBC, which forms the basis of all other frameworks.

The first section will focus on the Data Access module (or DAO module) and the services it provides for using JDBC. The second and third sections will detail the steps required to use the DAO module of the Spring Framework. In the last section, an application will be developed that will make use of the steps detailed in the second section to access and persist data.

Using JDBC with Spring

JDBC support in the Spring Framework is part of the data access/persistence (also known as DAO) support provided by the framework. Hence, it is better to know about DAO support before going into the JDBC aspects. The most important feature of DAO support is consistency. The following are the two ways in which the Spring Framework provides consistency:

1. Consistency in Exception Hierarchy

2. Consistency in Abstract classes

The latter of the two is more helpful when using different data persistence frameworks such as Hibernate, JPA etc. Here are the details.

Consistency in Exception Hierarchy: The Spring Framework provides a translation from technology-specific exceptions such as Hibernate-related exceptions or JDBC-related exceptions by having its own hierarchy of data access-related exceptions. The DataAcessException class forms the root of this hierarchy. These exceptions are runtime exceptions instead of checked exceptions (such as Hibernate's proprietary exceptions). This helps the developer to handle the non-recoverable errors in the appropriate layer instead of handling them at DAO level.

Consistency in Abstract classes: The Spring Framework provides a set of abstract classes that one can extend (not implement). There is one abstract class each for various data persistence technologies. These abstract classes have methods that can be used to set the required configurable properties that are specific to each technology. The main abstract classes are:

  • JdbcDaoSupport, which is the super class for accessing data using JDBC. You must provide a DataSource instance to it. This class, in turn, provides the JdbcTemplate instance, created from the supplied DataSource instance, to the sub-class.
  • HibernateDaoSupport, which is the super class for Hibernate data access objects. One has to supply an  instance of SessionFactory and in turn, it provides an instance of the HibernateTemplate class.

Apart from these, there are abstract classes for JDO, iBatis as well as JPA.

Now that the DAO support of the Spring Framework has been introduced, let us move on to the focus of this discussion - Spring's support for JDBC. Spring supports JDBC through the JdbcTemplate class. However, before using the services of this class you need to understand the different "approaches" you can take in using the JdbcTemplate.

These approaches are based on the types of JdbcTemplate. The main types or sub-classes of JdbcTemplate are JdbcTemplate, NamedParameterJdbcTemplate and SimpleJdbcTemplate. Of the JdbcTemplate is the most commonly used type. Here are the details.

  • JdbcTemplate is the most widely used of all the types or flavors. It is the 'lowest-level' type. In other words, all other flavors make use of JdbcTemplate as their basis.
  • NamedParameterJdbcTemplate provides a wrapper for Named Parameter queries of JDBC. It wraps the JdbcTemplate to provide a more convenient way of handling parameterized queries instead of the conventional "?" placeholder provided by JDBC, thus making it easier to use multiple parameters. 
  • SimpleJdbcTemplate combines the most commonly used features of JdbcTemplate and NamedParameterJdbcTemplate. It also makes use of new features of Java 5 such as varargs, generics etc.

Apart from these, there are other flavors, including SimpleJdbcCall and SimpleJdbcInsert, that reduce the configuration required. That completes the overview of Spring Framework's DAO support and JDBC support.

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