Java
  Home arrow Java arrow Page 3 - 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? 
Google.com  
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 - Configuring the Bean
    ( Page 3 of 4 )

    Configuring the bean means providing a declaration for injecting the DataSource to the POJO or bean. It is done using the <ref> child tag of the <property> tag. The <ref> tag tells the Spring Framework that the current bean or POJO is referencing another bean. And the <property> tag tells the instance variable that it will receive the reference.

    For example, the following statements tell the Spring Framework that the bean with the ID "dao" refers the bean with the of ID"dataSource." The implementation of DataSource will be received by the DataSource instance variable within the JdbcEventDao class. The following is the code for the same:


    <bean id="dao" class=" JdbcEventDao ">

    <property name="dataSource">

    <ref local="dataSource"/>

    </property>

    </bean>


    Finally, we are up to the third step, developing the client. To develop the client, you must do two things. First, you must retrieve an instance of the JdbcTemplate class. To do so, an implementation of DataSource needs to be retrieved using JdbcEventDao POJO. The following statements will retrieve the DataSource implementation as well as a JdbcTemplate instance:


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

    BeanFactory factory = new XmlBeanFactory(res);

    JdbcEventDao bean = (datacon)factory.getBean("dao");

    DataSource ds=bean.getDataSource();

    JdbcTemplate template = new JdbcTemplate(ds);

     

    Second, the instance of the JdbcTemplate can be used to perform a CRUD (Insert, Select, Update, Delete) operation. For example, to retrieve all the records from "emp" table, the statement will be


    List list;

    list = template.queryForList("select * from emp");


    The queryForList returns a list containing the values of the resultset.


    That brings us to the end of this section. In the next section, an application will be developed based on these steps.



     
     
    >>> 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
    For more Enterprise Application Development news, visit eWeek