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