While working with database using plain old JDBC, it becomes cumbersome to write unnecessary code to handle exceptions, opening and closing database connections etc. But Spring JDBC Framework takes care of all the low-level details starting from opening the connection, prepare and execute the SQL statement, process exceptions, handle transactions and finally close the connection.

 

Spring JDBC provides several approaches and correspondingly different classes to interface with the database

The Spring Framework’s JDBC abstraction framework consists of four different packages, namely

core

datasource

object

support

1.Core

org.springframework.jdbc.core.

SubPackage:

1.1 org.springframework.jdbc.core.simple

1.2 org.springframework.jdbc.core.namedparam

  1. Datasource

org.springframework.jdbc.datasource

SubPackage:

2.1 org.springfamework.jdbc.datasource.embedded

3.object

org.springframework.jdbc.object

4.support

org.springframework.jdbc.support

 

org.springframework.jdbc.core.

This package contains the JdbcTemplate class and its various callback interfaces, plus a variety of related classes.

It’s subpackage named org.springframework.jdbc.core.simple contains the SimpleJdbcInsert and SimpleJdbcCall classes. Another subpackage named org.springframework.jdbc.core.namedparam contains the NamedParameterJdbcTemplate class and the related support classes.

 

The org.springframework.jdbc.datasource package contains a utility class for easy DataSource access, and various simple DataSource implementations that can be used for testing and running unmodified JDBC code outside of a Java EE container. A subpackage named org.springfamework.jdbc.datasource.embedded provides support for creating in-memory database instances using Java database engines such as HSQL and H2.

 

Here, we are using the JdbcTemplate object in the EmployeeDao class, so we are passing it by the setter method but you can use constructor also.

<?xml version=”1.0″ encoding=”UTF-8″?>

<beans

xmlns=”http://www.springframework.org/schema/beans”

xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

xmlns:p=”http://www.springframework.org/schema/p”

xsi:schemaLocation=”http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd”>

<bean id=”ds” class=”org.springframework.jdbc.datasource.DriverManagerDataSource”>

<property name=”driverClassName” value=”oracle.jdbc.driver.OracleDriver” />

<property name=”url” value=”jdbc:oracle:thin:@localhost:1521:xe” />

<property name=”username” value=”system” />

<property name=”password” value=”oracle” />

</bean>

<bean id=”jdbcTemplate” class=”org.springframework.jdbc.core.JdbcTemplate”>

<property name=”dataSource” ref=”ds”></property>

</bean>

<bean id=”edao” class=”com.helical.hdidao”>

<property name=”jdbcTemplate” ref=”jdbcTemplate”></property>

</bean>

</beans>

Leave a Reply