Spring JDBC Introduction

Hi friends, focus to this concept before going to spring JDBC. Here i am presenting the detail introduction to spring JDBC.

We know that we use JDBC technology for persistency purpose, that means connecting with database and performing some operations like insert, update, delete etc. Without using JDBC we cannot able to connect with databases.

But there are some difficulties if a java programmer is directly working with JDBC which is as follows:

  • JDBC program coming under checked exceptions, that means we must use try, catch blocks which increases the complexity of the application.
  • In JDBC, programmer is always responsible to open and close the connection, otherwise connection issue that we may get or some memory leakage issue may come.
  • Another problem is boiler plate code that means duplicate code we need to write each and every time like we may need to write loading driver, connection, creating statement so many times.
  • In JDBC, some database related error code may raise like ORA-666. This error code is not understandable by the java programmer. So this is one of the main problem.

To eliminate these above problems, Spring framework has provided one abstraction layer on top of existing JDBC called Spring-JDBC. Here programmers will work with this abstraction layer and that layer will internally uses JDBC technology. So spring-JDBC layer will take cares about connection management and error managements, and programmers will concentrate on their logics.

There is an exception translator provided by spring framework which translates the checked exceptions to un-checked exceptions of spring type and finally the un-checked exceptions are thrown to java programmer. Another advantage is while working with spring-JDBC, the programmer is not required to open and close the database connection, because that will be taken care by the spring framework.

There are two ways to get connection with database like,

  • By using java.sql.DriverManager [ Class ]
  • By using javax.sql.DataSource [ Interface ]

Spring framework uses DataSource interface to obtain the connection with database internally, there are two implementation classes and we can use any one of the following classes of DataSource interface.

  • Org.springframework.jdbc.datasource.DriverManagerDataSource [ class ]
  • Org.apache.commons.dbcp.BasicDataSource [ class ]

Here DriverManagerDataSource is given by spring framework and it is equal to DriverManager class, 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 configuration file, the following 4 properties that we have to write to get connection with database. The code is like:

<bean id="id2" class="org.springframework.datasource.DriverManagerDataSource">
or
<bean id="id2" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value=" " />
<property name="url" value=" " />
<property name="username" value=" " />
<property name=password" value=" " />
</bean>

Our Services:

Java Training | Python Training | Machine Learning with Python Training | Deep Learning with Python Training | Projects

SILAN Technology
Call to: 9439202111  |  9658317757  |  0674-2361252

Comment