Java
  Home arrow Java arrow Page 4 - 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 - JdbcTemplate in the Real World
    ( Page 4 of 4 )

    The application being developed will perform two functions. First, it will retrieve data related to a given ID. Second, it will delete the data related to a given ID. It is a simple application, but it will give you an idea of how JDBC and the Spring Framework work together. The application will have three main files:


    1. JdbcDataSource.java - The POJO having getter and setter for the DataSource reference.
    2. beans.xml - The configuration file for the Spring Framework.
    3. JdbcClient.java - A client that makes use of JdbcTemplate for retrieval and deletion functionalities.

    Let us start with JdbcDataSource.java. It is same as the JdbcEventDao class. The following is the code:


    public class JdbcDataSource

    {

    private DataSource dataSource;

    public void setDataSource(DataSource dataSource)

    {

    this.dataSource = dataSource;

    }

     

    public DataSource getDataSource()

    {

    return dataSource;

    }

    }


    Next comes the beans.xml. It contains the DataSource configuration as well as the bean configuration.


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

     

    <property name="driverClassName">

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

    </property>


    <property name="url">

    <value>jdbc:mysql://localhost:3306/requisition</value>

    </property>


    <property name="username">

    <value>root</value>

    </property>


    <property name="password">

    <value>sql</value>

    </property>


    </bean>


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

    <property name="dataSource">

    <ref local="dataSource"/>

    </property>

    </bean>


    And at last we've reached the client. It takes command line arguments and, according to the first argument, either retrieves or deletes using the second argument as the ID. Here is the code.


    import java.io.*;

    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 JdbcClient

    {

    public static void main(String args[]) throws Exception

    {

    try

    {

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

    BeanFactory factory = new XmlBeanFactory(res);

    JdbcDataSource bean = (JdbcDataSource)factory.getbean("dao");


    JdbcTemplate template = new JdbcTemplate(bean.getDataSource());

     

    if("select".equalsIgnoreCase(args[0]))

    {

    List list;

    list = template.queryForList("select * from indents where"+
    " indent_id ='"+args[1]+"'");

    Iterator i=list.iterator();


    while(i.hasNext())

    {

    Object ob = i.next();

    out.println(ob.toString());

    }

    }

     

    if("delete".equalsIgnoreCase(args[0]))

    {

    template.execute("Delete from indents where indent_id ='" +
    args[1]+"'");

    }

     

    }catch(Exception e)

    {

    }

    }

    }


    The delete functionality makes use of the execute method of the JdbcTemplate class. This method can be used to execute other SQL statements including inserts and updates.

    That completes this section as well as brings us to the end of this discussion. The approach discussed here is just one of the many approaches to using the Spring Framework's JDBC support. There are other approaches for the same. Those will be discussed in the future. Till then...



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