Over a million developers have joined DZone.

Spring SimpleJdbcTemplate Tutorial

Build fast, scale big with MongoDB Atlas, a hosted service for the leading NoSQL database. Try it now! Brought to you in partnership with MongoDB.

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.

package com.vaannila.dao;

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;

import com.vaannila.domain.Forum;

public class ForumDAOImpl implements ForumDAO {

	private SimpleJdbcTemplate simpleJdbcTemplate;

	public void setDataSource(DataSource dataSource) {
		this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource);
	}

	@Override
	public void insertForum(Forum forum) {
		String query = "INSERT INTO FORUMS (FORUM_ID, FORUM_NAME, FORUM_DESC) VALUES (?,?,?)";
		simpleJdbcTemplate.update(query, forum.getForumId(), forum
				.getForumName(), forum.getForumDesc());
	}

	@Override
	public Forum selectForum(int forumId) {
		String query = "SELECT * FROM FORUMS WHERE FORUM_ID=?";
		return simpleJdbcTemplate.queryForObject(query, new ParameterizedRowMapper<Forum>() {
					@Override
					public Forum mapRow(ResultSet resultSet, int rowNum) throws SQLException {
						return new Forum(resultSet.getInt("FORUM_ID"), resultSet.getString("FORUM_NAME"), 
                        			resultSet.getString("FORUM_DESC"));
					}
				}, forumId);
	}

}

The following snippet shows the insertForum() method using the JDBCTemplate.

public void insertForum(Forum forum) {
    String query = "INSERT INTO FORUMS (FORUM_ID, FORUM_NAME, FORUM_DESC) VALUES (?,?,?)";
    jdbcTemplate.update(query, new Object[] { Integer.valueOf(forum.getForumId()),
            forum.getForumName(), forum.getForumDesc() });
}

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 insertForum(Forum forum) {
    String query = "INSERT INTO FORUMS (FORUM_ID, FORUM_NAME, FORUM_DESC) VALUES (?,?,?)";
    simpleJdbcTemplate.update(query, forum.getForumId(), forum
            .getForumName(), forum.getForumDesc());
}

The following snippet shows the selectForum() method using the JDBCTemplate.

public Forum selectForum(int forumId) {
    String query = "SELECT * FROM FORUMS WHERE FORUM_ID=?";
    return (Forum) jdbcTemplate.queryForObject(query, new Object[] { Integer.valueOf(forumId) }, 
            new RowMapper() {
                public Object mapRow(ResultSet resultSet, int rowNum) throws SQLException {
                    return new Forum(resultSet.getInt("FORUM_ID"), resultSet.getString("FORUM_NAME"), 
                            resultSet.getString("FORUM_DESC"));
                }
            });
}

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 FORUMS WHERE FORUM_ID=?";
    return simpleJdbcTemplate.queryForObject(query, new ParameterizedRowMapper<Forum>() {
                @Override
                public Forum mapRow(ResultSet resultSet, int rowNum) throws SQLException {
                    return new Forum(resultSet.getInt("FORUM_ID"), resultSet.getString("FORUM_NAME"), 
                                resultSet.getString("FORUM_DESC"));
                }
            }, forumId);
}

You can download and try the example here.

Source :Download
Source + Lib :Download

 

 




Now it's easier than ever to get started with MongoDB, the database that allows startups and enterprises alike to rapidly build planet-scale apps. Introducing MongoDB Atlas, the official hosted service for the database on AWS. Try it now! Brought to you in partnership with MongoDB.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}