Spring Framework
Spring Core with Data Access Framework

In this article, we will move on to the main process of any enterprise application: Data Persistence. For this we have to initialize our data access framework, manage resources, handle various exceptions and if anything goes wrong, we must roll-back so as to save the existing data.

Spring comes with a family of data access frameworks that integrates well will variety of data access technologies like JDBC, Java Data Objects and Object Relational Mapping (ORM) tools like Hibernate, OJB, iBatis etc.,

Many JEE application servers and even web servers provide a ‘dataSource’ via JNDI name. To configure the spring bean with the JNDI name of our ‘dataSource’ and use its connection pooling facility, ‘JndiObjectFactoryBean’ is used. When a DataSource is not present, we need a connection pooling bean that implements ‘dataSource’. For this purpose we use dbcp.BasicDataSource’. By using this we can have a ‘dataSource’ with connection pooling independent of application server.

To perform unit-tests in our data access code, spring comes with a very lightweight ‘dataSource’ implementation class: ‘DriverManagerDataSource’. This class can be easily configured for unit tests as shown:

DriverManagerDataSource dataSource =
new DriverManagerDataSource();


These properties can be configured in the spring configuration file also. Spring’s own Data Access Framework Spring comes with its own data access framework. Spring separates the fixed and variant parts of data access process into two distinct classes: template and callback. Template manages the fixed part of our framework like data connection, managing resources, controlling transaction etc., while the Callback defines the things that are specific to our application like creating statements, binding parameters etc., The template class of Spring is ‘JdbcTemplate’. A ‘dataSource’ is provided inside JdbcTemplate.

An example of database connection using ‘JdbcTemplate’ is shown below.

Here we are using ‘MySql’ database. The MySql database can be downloaded from Download latest MySql version and mysql-connector-java-3.1.6-bin.jar. Install them in the hard disk. Installing MySql:

1. For Mysql give a username(‘root’) and a password (‘root’).

2. Then start the ‘My Sql Console Line Client’ from programs and type the password. The prompt will be changed to mysql,mysql> show databases; Few databases will be present by default like mysql and test. Let’s use ‘test’ for our purpose.mysql> use test;

3. We will get a message as ‘Database changed’.

4. Next create a table in ‘test’ database as follows
mysql> create table table1(name text, place text);

Sept 2007 | Java Jazz Up | 55
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,   Download PDF