Share on Google+Share on Google+

Spring JDBC Introduction

This tutorial provide a brief introduction about Spring DAO JDBC.

Spring JDBC Introduction

The Spring's DAO(Data access object) make it easy for us to use data access technologies like JDBC, Hibernate, JPA or JDO for accessing data from database. Using DAO you can switch any of the above persistence technology without any overhead. Also you don't need to worry about catching exception that are related to each technology.

This tutorial provide a brief introduction about Spring DAO JDBC. The following table  describe which actions are developer's responsibility and which actions are taking care by Spring itself :

Actions & responsibility

Actions Developer     Spring 
Define parameters for connection        Y      
connection opening        Y 
Specify the statement for SQL       Y   
Declaration of parameters & providing
value for parameter.
Prepare Statement & execute        Y 
Loop setup for result iteration(if required)        Y 
Defining work for each iteration       Y   
exception handling        Y 
Handling transactions        Y 
Connection, statement & resultset closing        Y 

Different approaches for database access

JDBC 2.0 is required by below given approaches & some advance feature needed JDBC 3.0 driver .Spring 3.0 also supports java 5 features such as generics and varargs. Spring 3.0 supports following approaches :

1. Jdbc Template :

This is one of the most popular approach..This 'lowest level' approach  use a JdbcTemplate under the covers . And it is also updated with java 5 features such as generics and varargs.

2. NamedParameterJdbcTemplate

This approach also has a JdbcTemplate. This template is useful where we have  many parameters in a SQL statement. It supply named parameter instead of conventional "?".

3. SimpleJdbcTemplate

This Template aggregate the most often used procedures of the JdbcTemplate and NamedParameterJdbcTemplate.

4. SimpleJdbcInsert and SimpleJdbcCall

This approach minimize the database metadata. This optimization is to bound the amount of necessary configuration. In this approach you need to provide name of table and a map of parameters matching the column name. This works only if database provide sufficient metadata. In case it is not provided adequately, then you have to provide explicit configuration of the parameters.

5. RDBMS Objects (MappingSqlQuery, SqlUpdate and StoredProcedure )

Firstly you have to define JDO query ,where you define your query ,declare parameters & compile it. After this you have to create reusable & thread safe objects. This is done during initialization of the data access layer. After completing all this, we can executes methods many times by passing various parameters' values.

JDBC Framework's Packages

JDBC Framework's package contain 4 different package - core, datasource , object & support.

org.springframework.jdbc.core - This package have JdbcTemplate class and its various callback interfaces .

org.springframework.jdbc.datasource - This package have the utility class. This class is used for easy data source access and implementations. These implementations can be use for testing and running unmodified JDBC code outside of a J2EE container.

org.springframework.jdbc.object  - This package contains classes that represent RDBMS queries, updates, and stored procedures as thread safe, reusable objects. - This package provides SQLException translation functionality and some utility classes.



Posted on: August 18, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.


Advertisement null