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);        
    }
    
}