JdbcTemplate class in spring JDBC

JdbcTemplate class present in org.springframework.jdbc.core.* package and provides some methods for executing the SQL statements on a database.

JdbcTemplate class having 3 methods to execute SQL operations on the database like

  1. execute()
  2. update()
  3. query()

execute() and update() methods are used for performing non-select operations on the database, and query method is performing for querying(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.

JdbcTemplate class with Constructor injection

If we insert DriverManagerDataSource object into JdbcTemplate class with constructor injection, then the spring configuration file will be :

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

Download Code

JdbcTemplate class with setter injection

If we insert DriverManagerDataSource object into JdbcTemplate class with setter injection, then the spring configuration file will be :

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

 

Our Services:

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

SILAN Technology, BBSR
Call to: 0674-2361252

Comment