Java
  Home arrow Java arrow Page 2 - Data Access Using Spring Framework JDBC
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Data Access Using Spring Framework JDBC
By: A.P.Rajshekhar
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2008-12-16


    Table of Contents:
  • Data Access Using Spring Framework JDBC
  • Using JdbcTemplate Step-by-Step
  • Configuring the Bean
  • JdbcTemplate in the Real World

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Data Access Using Spring Framework JDBC - Using JdbcTemplate Step-by-Step
    ( Page 2 of 4 )

    Next let us see how to use Spring's JDBC support. Since JdbcTemplate is the 'lowest-level' of all the types, the steps required for JdbcTemplate become part of the steps for all other types. The following are the steps required to make use of JdbcTemplate:

    1. Develop the bean

    2. Configure the bean and DataSource

    3. Develop the client

    Of these, the second and third steps can be divided into sub-steps. Here are the details.


    Develop the bean: The bean or POJO is similar to any other POJO used with the Spring Framework except for one difference. The POJO developed to be used with JdbcTemplate requires a setter for the DataSource object. The following is an example of a POJO that can be used with JdbcTemplate. The setter will pass the DataSource object to the instance of JdbcTemplate.

     

    public class JdbcEventDao

    {

    private DataSource dataSource;

    public void setDataSource(DataSource dataSource)

    {

    this.dataSource = dataSource;

    }

     

    public DataSource getDataSource()

    {

    return dataSource;

    }

    }


    The POJO can contain other methods that can work with the DataSource.

    Configure the DataSource and the bean: The configuration is done using the XML file. Lets call it beans.xml. This step can be further divided into configuring the DataSource, and configuring the bean. We will look at the DataSource first. 

    <>

    A DataSource is configured by declaring it as a bean and providing the required information as the child nodes of the bean declaration. The configuration is done as follows:

    First, a bean is declared whose class is mapped to an implementation of
    DataSource. One of the commonly-used implementations is org.apache.commons.dbcp.BasicDataSource. For example, to declare a bean with its class mapped to BasicDataSource class as the DataSource implementation class, the statement will be


    <bean id="dataSource" destroy-method="close"

    class="org.apache.commons.dbcp.BasicDataSource">

    :

    :

    </bean>


    Second, the required details for the DataSource such as driver name, URL of the DataSource, credentials etc. can be passed onto the DataSource through property tags. The property tags are the children of the <bean> tag. In versions prior to 2.5, the property tags had the <value> tag as the child tag. For example, to pass "com.mysql.jdbc.Driver" as the value of a property named "driverClassName," the statement in version 2.5 and above will be


    <bean id="dataSource" destroy-method="close"
    class="org.apache.commons.dbcp.BasicDataSource">

    <property name="driverClassName"
    value="com.mysql.jdbc.Driver"/>

    </bean>


    and in previous versions it will be


    <bean id="dataSource" destroy-method="close"
    class="org.apache.commons.dbcp.BasicDataSource">

    <property name="driverClassName">

    <value> com.mysql.jdbc.Driver</value>

    </property>

    </bean>


    The next step is configuring the bean.



     
     
    >>> More Java Articles          >>> More By A.P.Rajshekhar
     

       

    JAVA ARTICLES

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT