Java & J2EE Data Access Using Spring Framework JDBC |
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:
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.
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.
blog comments powered by Disqus |