Spring JdbcTemplate and Spring SimpleJdbcTemplate difference with Example

To use the SimpleJdbcTemplate you need to use JDK 1.5 or higher. SimpleJdbcTemplate takes advantage of the Java 5 language features like varargs, autoboxing, generics and covariant returns.

Following example shows you how to use SimpleJdbcTemplate and advantage of it over JdbcTemplate
package com.anuj.spring.db;

import java.sql.ResultSet;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

/**
 *
 * @author Anuj J Patel
 */
public class ForumDAOSimpleJDBCImpl implements ForumDAO{

    /**
     * Simple JDBCTemplate
     * 1.When using SimpleJDBCTemplate there is no need to typecast from int to Integer
     * and you can pass variable length arguments instead of Object array
     * 2.There is no need to typecast return object, parameterizedRowMapper object's type parameter 
     * will be taken by default
     */
    private SimpleJdbcTemplate simpleJDBCTemplate;
    
    public void setDataSource(DataSource dataSource){
        this.simpleJDBCTemplate = new SimpleJdbcTemplate(dataSource);
    }
    
    @Override
    public void insert(Forum forum) {
        String query = "insert into tbforum(forumId,forumName,forumDesc) values(?,?,?)";
        simpleJDBCTemplate.update(query, forum.getForumId(),forum.getForumName(),forum.getForumDesc());
        System.out.println("Record Inserted successfully");
    }

    @Override
    public Forum selectForum(int forumId) {
        String query="select * from tbforum where forumId=?";
        
        return simpleJDBCTemplate.queryForObject(query, 
                new ParameterizedRowMapper(){
                        @Override
                        public Forum mapRow(ResultSet rs, int rowNum) throws SQLException {
                            return new Forum(rs.getInt("forumId"),rs.getString("forumName"), rs.getString("forumDesc"));
                        };               
                },forumId);        
    }
    
}


1. Spring JdbcTemplate :
The following snippet shows the insertForum() method using the JDBCTemplate.

 @Override
    public void insert(Forum forum) {
        String query = "insert into tbforum(forumId,forumName,forumDesc) values(?,?,?)";
        jdbcTemplate.update(query, new Object[]{
            forum.getForumId(),forum.getForumName(),forum.getForumDesc()
        });
        System.out.println("Record Inserted successfully");
    }

Spring SimpleJdbcTemplate - Advantage: When using SimpleJDBCTemplate you can use the variable length arguments instead of an Object array. There is no need to explicitly typecast int to Integer.

@Override
    public void insert(Forum forum) {
        String query = "insert into tbforum(forumId,forumName,forumDesc) values(?,?,?)";
        simpleJDBCTemplate.update(query, forum.getForumId(),forum.getForumName(),forum.getForumDesc());
        System.out.println("Record Inserted successfully");
    }

 2. Spring JdbcTemplate : The following snippet shows the selectForum() method using the
JDBCTemplate.

 @Override
    public Forum selectForum(int forumId) {
        String query="select * from tbforum where forumId=?";
       
        return (Forum)jdbcTemplate.queryForObject(query, new Object[]{forumId}, 
                new RowMapper(){
                        @Override
                        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
                            return new Forum(rs.getInt("forumId"),rs.getString("forumName"), rs.getString("forumDesc"));
                        };              
                });       
    }

Spring SimpleJdbcTemplate  - Advantage: Here when using SimpleJDBCTemplate you need not explicitly typecast the return object, the ParameterizedRowMapper object's type parameter will be taken by default. The return type of the mapRow() method is Forum instead of Object because in Java 5 you can have covariant return types. Since the statement parameter can be of variable length the forumId is specified at the end of the list.

 @Override
    public Forum selectForum(int forumId) {
        String query="select * from tbforum where forumId=?";
       
        return simpleJDBCTemplate.queryForObject(query, 
                new ParameterizedRowMapper(){
                        @Override
                        public Forum mapRow(ResultSet rs, int rowNum) throws SQLException {
                            return new Forum(rs.getInt("forumId"),rs.getString("forumName"), rs.getString("forumDesc"));
                        };              
                },forumId);       
    }

How to run : In order to run this, you can use following code in Main Java Class and execute require method using forumDAOObject.

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
 ForumDAO forumDAO = (ForumDAO) context.getBean("forumDAO")

For any information regarding classed used in this demo. Please refer to here

Enjoy Coding :)

No comments:

Post a Comment