Skip to main content

Why Spring Jdbc module !

To over the problems spring has introduced Spring Jdbc module, there are plenty of advantages of using spring jdbc over java jdbc.

There are #5 different ways we can perform database operations while working with spring jdbc 

1. JdbcTemplate approach

2. NamedParameterJdbcTemplate approach

--------------------------------------------- (deprecated and removed in spring 5.0+)

3. SimpleJbdcTemplate approach

4. SimpleJdbcInsert or SimpleJdbcCall

5. Mapping sql operations as Subclasses

  • out of the above #2 of them are popularly used in the market .
1. JdbcTemplate Approach
JdbcTemplate is the core component/class of spring jdbc module, that has been wrapped with necessary logic in performing database operations on the underlying database tables. We dont need to use java jdbc api classes like Connection, Statement etc in performing database operations, we just need to use JdbcTemplate itself.

Again in JdbcTemplate there are #2 approaches are there in performing database operations.
1. Classic Approach = Classic approach exactly resembles building application while working with java jdbc api.
2. Query-based Approach = the modern way of building database applications using JdbcTemplate where it eliminates most of the boiler-plate logic.here we completely eliminate boiler-plate logic.

Irrespective of any of these approaches there is some common/infrastructure code we need to write in every spring jdbc application as below.
class StudentDao {
private JdbcTemplate jdbcTemplate;
public StudentDao(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}

JdbcTemplate inorder to perform the database operation, it requires Connection as an input. So we need to configure DataSource object with database information and inject it as an input to the JdbcTemplate, so that JdbcTemplate can grab the Connection from the DataSource whenever needed to perform operation

javax.sql.DataSource = A DataSource object represents a pool of Connections to a specific database. is an interface provided aspart of jee api. The JEE Application Servers (like weblogic, websphere, Glassfish Server etc) has provided implementations for the DataSource interface.
Looks like to work with Spring jdbc we need Application Server support, since the DataSource implementation is provided aspart of the ApplicationServer. But there are third-party standalone implementations are also available for DataSource interface like
1. Proxool
2. Dbcp
3. Hikari
4. C3P0
etc
These vendors has provided their own implementations for the DataSource interface, that can be directly used and configured for our application.
Additionally Spring Jdbc also has provided their own implementation of DataSource interface which is "DriverManagerDataSource", that can be directly configured to be used as an DataSource implementation object in our application.

Note: DriverManagerDataSource is not an fully implemented DataSource Connection Pool and should be used only for development purpose only.
application-context.xml
-----------------------

<bean id="dataSource" class="DriverManagerDataSource">
<property name="driverClassname" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/sdb"/>
<property name="username" value="root"/>
<property name="password" value="welcome1"/>
</bean>

<bean id="jdbcTemplate" class="JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>

<bean id="studentDao" class="StudentDao">
<constructor-arg ref="jdbcTemplate"/>
</bean>


Comments

© 2020 The JavaDS Hub

Designed by Open Themes & Nahuatl.mx.