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

Configuring the Bean - 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.

  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



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



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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