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
|Define parameters for connection||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|
|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.
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 "?".
This Template aggregate the most often used procedures of the JdbcTemplate and NamedParameterJdbcTemplate.
4. SimpleJdbcInsert and SimpleJdbcCallThis 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.
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.
org.springframework.jdbc.support - 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