Spring Framework Part III

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

Spring Framework Part III


by Farihah Noushene B.E.,




Now 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 J2EE 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' is used. 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,

DriverManagerDataSource  dataSource = new   DriverManagerDataSource();





These properties can be configured in the spring configuration file also.


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 http://www.mysql.com. Download mysql4.1 and MyODBC-3.51 (ODBC Connector) install these in the hard disk. For Mysql give a username('root') and a password ('sql').

Then start the 'My Sql Console Line Client' from programs and type the password.

The prompt will be changed to mysql,

mysql> show databases;

Two databases will be present default: mysql and test.

mysql> use test;

We will get message as 'Database changed'. Next create table in test database as follows

mysql> create table table1(name text, place text);

We will get the message 'Query OK, 0 rows affected'. Now we have created a table in mysql database, set the path and classpath as before  and edit the program


import javax.sql.*;

public interface datacon


  public DataSource dbcon();




import org.springframework.jdbc.core.*;

import org.springframework.jdbc.datasource.*;

import org.springframework.jdbc.object.*;

import org.springframework.jdbc.support.*;

import javax.sql.*;

public class dataconimpl implements datacon


   private DataSource dataSource;

   public void setDataSource(DataSource ds)


  dataSource = ds;


   public DataSource dbcon()


  return dataSource;





<?xml version="1.0" encoding="UTF-8"?>




  <bean id="dataSource"


  <property name="driverClassName">



  <property name="url">



  <property name="username">



  <property name="password">




  <bean   id="datacon"  class="dataconimpl">

  <property name="dataSource">

  <ref local="dataSource"/>






import java.io.*;

import javax.sql.*;

import java.sql.*;

import java.util.*;

import javax.servlet.*;

import javax.servlet.http.*;

import org.springframework.beans.factory.*;

import org.springframework.beans.factory.xml.*;

import org.springframework.core.io.*;

import org.springframework.jdbc.core.*;

import org.springframework.jdbc.datasource.*;

import org.springframework.jdbc.object.*;

import org.springframework.jdbc.support.*;

public class springservlet extends HttpServlet


   public void doPost(HttpServletRequest req,HttpServletResponse resp)

  throws ServletException,IOException     



  PrintWriter out = resp.getWriter();

  String a = req.getParameter("text1");

  String b = req.getParameter("text2");

  String c = req.getParameter("combo1");

  String d = req.getParameter("combo2");




   Resource  res = new ClassPathResource("datacon.xml");

   BeanFactory  factory = new   XmlBeanFactory(res);

   datacon bean1 = (datacon)factory.getBean("datacon");

   DataSource ds=bean1.dbcon();



  JdbcTemplate   jt = new  JdbcTemplate(ds);

  jt.execute("insert into table1 values('"+a+"','"+b+"') ");

  out.println("Record Added");




  JdbcTemplate   jt = new JdbcTemplate(ds);

  jt.execute("delete from table1 where name='"+a+"' ");

  out.println("Record Deleted");




  List  list1;

  JdbcTemplate   jt = new  JdbcTemplate(ds);

    list1=jt.queryForList("select * from table1 where name='"+a+"'");

    Iterator i=list1.iterator();



   Object  ob = i.next();








   JdbcTemplate   jt = new  JdbcTemplate(ds);

   jt.execute("update table1 set table1.place='"+b+"'where

     table1.name='"+a+"' ");




     JdbcTemplate   jt = new  JdbcTemplate(ds);

   jt.execute("update table1 set table1.name='"+a+"'where

   table1.place='"+b+"' ");


   out.println("Record Updated");



  catch(Exception e1)







<body bgcolor="pink">

<form  method=post 



Name  :  <input type=text name="text1">  <br><br>

Place  :  <input type=text name="text2">  <br><br>

Criterion :

  <select name="combo1" size=1>

   <option value="name">Name

   <option value="place">Place

  </select>   <br><br>

  <select name="combo2" size=1>

   <option value="add">Add

   <option value="delete">Remove

   <option value="find">Find

   <option value="update">Update

  </select>   <br><br>

  <input type=submit>




The deployment procedure is same as before compile the all the files datacon.java, dataconimpl.java and springservlet.java and copy all the class files and the xml file datacon.xml  to g:\tomcat5\webapps\root\web-inf\classes. Copy the html file to g:\tomcat5\webapps\root. Add entry to web.xml file.

Start Tomcat server and open browser

and type url as http://localhost:8080/ servletclient.htm. We will get a two textboxes, two comboboxes and a 'submit' button. Type name and place in textboxes, select 'add' from combobox and click 'submit' button. We will get message as 'Record Added'

Spring provides integration for many of the ORM frameworks like Hibernate, JDO, Apache OJB and iBATIS SQL Maps.

For mapping the hibernate resources, an instance of 'SessionFactory' is needed, 'LocalSessionFactoryBean' is used for this purpose and its properties 'hibernateProperties', 'mappingResources' and 'mappingDirectoryLocation' are set. Like Spring's DAO framework, here we have 'HibernateTemplate' to create an object of 'SessionFactory'. To access the data with 'HibernateTemplate' 'execute(HibernateCallback)' method is used.

Similar to the 'SessionFactory' of hibernate, JDO has 'PersistenceManager Factory'. It can be configured by using 'LocalPersistenceManagerFactoryBean'. Also 'JDOTemplate' to create an object of 'PersistenceManagerFactory'. To access the data with 'JDOTemplate' 'execute(JDOCallback)' method is used.

For iBATIS, we have to configure a 'SQLMapClient' by using 'SQLMapClientFactoryBean' and its properties  'configLocation' and 'dataSource' are set. Here also we have 'SQLMapClientTemplate'.

To access the data 'execute(SQLMapClientCallback)' method is used.

The only property that we need to change to integrate Spring with OJB is 'ConnectionFactoryClass' and it is done by using 'LocalDataSourceConnectionFactory'.

In the next article we shall see how to use a RMI service in Spring and how to export any Spring managed bean as RMI.



  1. How much time will it take to learn Spring and Hibernate?
  2. Advantages of Spring Framework
  3. Spring 4 Introduction and example
  4. Features of Spring Framework 5
  5. How to make Spring web Login form?
  6. How to make Simple form in Spring web?
  7. Spring, Hibernate login and registration application
  8. Spring Framework Tutorial for beginners with examples
  9. Spring Framework for Apache Hadoop 2.3.0 GA released
  10. Spring Framework 4.1 GA is released with major features
  11. Why to use Spring Framework?
  12. Spring Framework 4.1 - First Release candidate available
  13. Spring IO Platform 1.0.0 Released
  14. Spring 4: Login Form using Spring MVC and Hibernate Example
  15. Spring 4 MVC Login form Example with source code
  16. Spring 4 MVC Hello World Example: Spring 4 MVC Tutorial will full source code
  17. Spring Web MVC Application Error:ClassNotFoundException: DispatcherServlet on deploying
  18. Features of Spring 4
  19. Spring Framework 4.0 released
  20. Spring Framework 4: Spring Framework 4 Tutorials and Example
  21. Spring Integration 3.0 Release is released and available for download
  22. Spring Tutorial for Beginners
  23. Java Springs Framework Tutorial
  24. Spring Architecture
  25. Spring Framework Tutorials
  26. database spring registration form
  27. Spring Login Example
  28. Roseindia Spring Tutorial
  29. Spring Tutorial
  30. Spring 3.2 MVC insert and retrieve blob from the database
  31. The hidden tag
  32. The errors tag
  33. net.roseindia.dao
  34. net.roseindia.service
  35. net.roseindia.model
  36. net.roseindia.controller
  37. Spring 3.2 MVC Hibernate Example
  38. Spring 3.2 MVC, Upload File in a specific folder
  39. Spring 3.2 MVC Form Handling
  40. The textarea tag