What is JdbcTemplate class used for in Spring's JDBC API?

What is JdbcTemplate class used for in Spring's JDBC API?

The JdbcTemplate class is the central class in the JDBC core package. It simplifies the use of JDBC since it handles the creation and release of resources. This class executes SQL queries, update statements or stored procedure calls, imitating iteration over ResultSets and extraction of returned parameter values. It also catches JDBC exceptions defined in the hierarchy of org.springframework.dao package. Code using the JdbcTemplate only need to implement callback interfaces, giving them a clearly defined contract. 
The PreparedStatementCreator callback interface creates a prepared statement given a Connection provided by this class, providing SQL and any necessary parameters. The JdbcTemplate can be used within a DAO implementation via direct instantiation with a DataSource  reference, or be configured in a Spring IOC container and given to DAOs as a bean reference.
Example:

JdbcTemplate  template = new JdbcTemplate(myDataSource);
A simple DAO class looks like this.
public class StudentDaoJdbc implements StudentDao {
          private JdbcTemplate jdbcTemplate;

     public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
          this.jdbcTemplate = jdbcTemplate;
     }
    // more code here
} 

By:Jalees
Date:2002-02-09 00:00:00

Post Your Answers


User Email:

User Name:
Answers:

Related Spring sub Links

Spring sub interview questions and answers for experienced and fresher