DZone
Database Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Database Zone > Spring NamedParameterJdbcTemplate Tutorial

Spring NamedParameterJdbcTemplate Tutorial

This article takes a look at a tutorial that explains Spring NamedParameterJdbctemplate and helps you specify the named parameters.

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 15, 12 · Database Zone · Tutorial
Like (5)
Save
Tweet
189.17K Views

Join the DZone community and get the full member experience.

Join For Free

The NamedParameterJdbcTemplate class helps you specify the named parameters instead of classic placeholder('?') argument. Named parameters improve readability and are easier to maintain.

package com.vaannila.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;

import com.vaannila.domain.Forum;

public class ForumDAOImpl implements ForumDAO {

private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

public void setDataSource(DataSource dataSource) {
    this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
}

public void insertForum(Forum forum) {
String query = "INSERT INTO FORUMS (FORUM_ID, FORUM_NAME, FORUM_DESC) VALUES (:forumId,:forumName,:forumDesc)";
Map namedParameters = new HashMap();
namedParameters.put("forumId", Integer.valueOf(forum.getForumId()));
namedParameters.put("forumName", forum.getForumName());
namedParameters.put("forumDesc", forum.getForumDesc());
namedParameterJdbcTemplate.update(query, namedParameters);
}

public Forum selectForum(int forumId) {
String query = "SELECT * FROM FORUMS WHERE FORUM_ID=:forumId";
SqlParameterSource namedParameters = new MapSqlParameterSource("forumId", Integer.valueOf(forumId));

return (Forum) namedParameterJdbcTemplate.queryForObject(query,
namedParameters, 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"));
}
});
}

}

The named parameter values can be specified using a map, where the parameter name serves as the key. You can also map the sql parameters using the MapSqlParameterSource class.

You can download and try the example here.

Source :
Source + Lib :


Spring Framework Download sql

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Deployment of Low-Latency Solutions in the Cloud
  • Open Source Security Risks
  • Top Soft Skills to Identify a Great Software Engineer
  • Choosing Between GraphQL Vs REST

Comments

Database Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo