Followers

springdao : jdbcTemplate class

jdbcTemplate class

It is the central class in the Spring JDBC support classes. 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 exception 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.


Let's see the methods of spring JdbcTemplate class.

S.No
Method
Description
1
public int update(String query)
is used to insert, update and delete records
2
public int update(String query,Object... args)
is used to insert, update and delete records using PreparedStatement using given arguments.
3
public void execute(String query)
is used to execute DDL query.

4
public T execute(String sql, PreparedStatementCallback action)
executes the query by using PreparedStatement callback.
5
public T query(String sql, ResultSetExtractor rse)
is used to fetch records using ResultSetExtractor.
6
public List query(String sql, RowMapper rse)
is used to fetch records using RowMapper.


Example of Spring JdbcTemplate

We are assuming that you have created the following table inside the Derby database.

Data Base Table MYEMP
create table employee( 
id number(10), 
name varchar2(100), 
salary number(10),
address varchar2(100) 
); 

Files required are
Employee.java
          This is the Bean class which contains 4 properties with constructors and setter and getters.

EmployeeDao.java
          This is the class where we are going to write the logic to interact with the data base.

Beans.xml
          This is the configuration file where we are going to write the configurations for data source, JdbcTemplate and Bean classes.
         
Test.java
          This is the class where we are going to write logic.

No comments:

Post a Comment