Thursday, 21 April 2016

How to use JdbcTemplate ?


JDBC can be used more efficiently with the help of a template class provided by spring framework called as JdbcTemplate.

Resource management and error handling is reduced a lot. So it leaves developers to write the statements and queries to get the data to and from the database.

Configuration
<bean id="jdbcTemplate
      class="org.springframework.jdbc.core.JdbcTemplate">
   <property name="dataSource">
       <ref bean="dataSource"/> 
   </property>
</bean>
<bean id="studentDao" class="StudentDaoJdbc">
   <property name="jdbcTemplate"> 
        <ref bean="jdbcTemplate"/>  
    </property>
</bean>
<bean id="courseDao" class="CourseDaoJdbc">
   <property name="jdbcTemplate">
       <ref bean="jdbcTemplate"/>
   </property>
</bean>

DAO class
public class StudentDaoJdbc implements StudentDao {
   private JdbcTemplate jdbcTemplate;
   public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
      this.jdbcTemplate = jdbcTemplate;
   }    
   more... // methods to use jdbcTemplate
}

No comments:

Post a Comment

Note: only a member of this blog may post a comment.