DIR Return Create A Forum - Home
---------------------------------------------------------
Java
HTML https://java.createaforum.com
---------------------------------------------------------
*****************************************************
DIR Return to: General Discussion
*****************************************************
#Post#: 8--------------------------------------------------
Spring JDBC
By: shekar Date: September 23, 2014, 7:56 am
---------------------------------------------------------
Problems of JDBC API:
1.JDBC technology exceptions are checked, so we must use try,
catch blocks in the code at various places which increases the
complexity of the application. And that to this may cause to
have lot of repetitive code to perform the database operations [
some thing like we may need to write loading driver, connection,
creating statement lot of times ]
2. In JDBC if we open the connection with database, we only
responsible to close that connection. If not we may get some
connection issues
3. If you see JDBC, it will throws error codes of the
database, when ever an exception is raised. In fact all java
programmers may or may not know these code right ?, that to
these error codes are different from one database to other
database, so finally our application is gonna be database
dependent
In order to overcome the above problems by using JDBC directly,
Spring framework has provided one abstraction layer on top of
existing JDBC technology. We used to call this layer as
Spring-JDBC.
A java application can get connection with database using
following 2 ways
By using java.sql.DriverManager [ Class ]
By using javax.sql.DataSource [ Interface ]
Spring framework uses DataSource interface to obtain the
connection with database internally, i mean we will use any one
of the following 2 implementation classes of DataSource
interface.
Org.springframework.jdbc.datasource.DriverManagerDataSource
[ class ]
Org.apache.commons.dbcp.BasicDataSource [ class ]
In above 2 classes DriverManagerDataSource is given by spring
framework and it is equal to DriverManager class, it means
spring framework internally opens a new connection and closes
the connection for each operation done on the database.
BasicDataSource is given the apache, and this is better than
DriverManagerDataSource because BasicDataSource having inbuilt
connection pooling implementation.
In spring config we need to configure the following 4 properties
to obtain connection with database
<bean id=”id1”
class=”org.springframework.datasource.DriverManagerDataSource”>
[ or ]
<bean id=”id1” class=” org.apache.commons.dbcp.BasicDataSource”>
<property name=”driverClassName” value=”” />
<property name=”url” value=”” />
<property name=”username” value=”” />
<property name=”password” value=”” />
</bean>
Spring framework provides following approaches for JDBC database
access:
JdbcTemplate
NamedParameterJdbcTemplate
SimpleJdbcTemplate
SimpleJdbcInsert and SimpleJdbcCall
JdbcTemplate Class In Spring-JDBC:
It is the central class in the Spring JDBC support classes.
JdbcTemplate class is given in org.springframework.jdbc.core.*
package
This class will provides methods for executing the SQL commands
on a database
It takes care of creation and release of resources such as
creating and closing of connection object etc. So it will not
lead to any problem if you forget to close the connection.
It handles the exception and provides the informative exception
messages by the help of excepion classes defined in the
org.springframework.dao package.
We can perform all the database operations by the help of
JdbcTemplate class such as insertion, updation, deletion and
retrieval of the data from the database.
JdbcTemplate class provided the following 3 type of methods to
execute SQL operations on the database
execute()
update()
query() methods….
execute and update methods are for non-select operations on the
database, and query method is for select operations on the
database.
JdbcTemplate class depends on DataSource object only, as it will
opens database connection internally with DataSource. So we must
give this DataSource object to JdbcTemplate, actually we have
both setter, constructor injections in JdbcTemplate class for
inserting DataSource object.
Spring config file if we insert DriverManagerDataSource object
into JdbcTemplate class with constructor injection:
<bean id=”id1”
class=”org.springframework.datasource.DriverManagerDataSource”>
<property name=”driverClassName” value=”” />
<property name=”url” value=”” />
<property name=”username” value=”” />
<property name=”password” value=”” />
</bean>
<bean id="id2"
class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="id1" />
<bean>
Spring config file if we insert DriverManagerDataSource object
into JdbcTemplate class with setter injection:
<bean id=”id1”
class=”org.springframework.datasource.DriverManagerDataSource”>
<property name=”driverClassName” value=”” />
<property name=”url” value=”” />
<property name=”username” value=”” />
<property name=”password” value=”” />
</bean>
<bean id="id2"
class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="id1" />
<bean>
Example of Spring JdbcTemplate:
We are assuming that you have created the following table inside
the Oracle10g database.
*****************************************************