Home arrow Java & J2EE arrow Page 4 - Data Access Using Spring Framework JDBC

JdbcTemplate in the Real World - Java

Persisting and accessing data forms one of the most routine yet core functionalities of any application. In the world of JEE, there are many APIs as well as frameworks to achieve to achieve the same. The Spring Framework is no exception. This article will explain how to use this framework for persisting and accessing data in your applications.

TABLE OF CONTENTS:
  1. Data Access Using Spring Framework JDBC
  2. Using JdbcTemplate Step-by-Step
  3. Configuring the Bean
  4. JdbcTemplate in the Real World
By: A.P.Rajshekhar
Rating: starstarstarstarstar / 17
December 16, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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 & J2EE Articles          >>> More By A.P.Rajshekhar
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: