Home Tutorial Spring Spring3 Jdbc NamedParameterJdbcTemplate RowMapper

 
 

Share on Google+Share on Google+
NamedParameterJdbcTemplate RowMapper
Posted on: August 23, 2010 at 12:00 AM
Advertisement
This section contains the description of RowMapper interface, using with NamedParameterJdbcTemplate ,with an example.

NamedParameterJdbcTemplate RowMapper

The RowMapper mapping each row to java object. Each row of ResultSet is mapped by this interface. The RowMapper object is stateless that's why it is reusable. You don't need to worry about exceptions because it is the responsibility of JdbcTemplate to caught exceptions and also handle it.

EXAMPLE

Description of the program :

The RowMapper interface is use here to map each row to ResultSet object using "maprow()" method. This method do not support   "next()" on ResultSet object. It only map values of the current row. Here we are inserting value using "NamedParameterJdbcTemplate". We are using here Hash Map to hold value and pass it to "NamedParameterJdbcTemplate" object. For display purpose we are using 'maprow' method to map each row to result object. It only map values of the current row.

Designation.java

package net.roseindia;

public class Designation {

	private int Id;
	private String name;
	private String Desig;

	public Designation(int Id, String name, String Desig) {
		this.Id = Id;
		this.name = name;
		this.Desig = Desig;
	}

	public int getId() {
		return Id;
	}

	public void setId(int Id) {
		this.Id = Id;
	}

	public String getname() {
		return name;
	}

	public void setname(String name) {
		this.name = name;
	}

	public String getDesig() {
		return Desig;
	}

	public void setDesig(String Desig) {
		this.Desig = Desig;
	}

	public String toString() {
		return Id + " " + name + " " + Desig;
	}

}

DesignationDAO.java

package net.roseindia;

import net.roseindia.Designation;

public interface DesignationDAO {

	public void insertDesignation(Designation dsg);

	public Designation selectDesignation(int Id);

}

DesignationQuery.java

package net.roseindia;

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 net.roseindia.Designation;

public class DesignationQuery implements DesignationDAO {

	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

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

	@SuppressWarnings("unchecked")
	public void insertDesignation(Designation dsg) {
		String query = "INSERT INTO DESIGNATION (ID, NAME, DESIGNATION) 
		VALUES (:Id,:name,:Desig)";
		Map namedParameters = new HashMap();
		namedParameters.put("Id", Integer.valueOf(dsg.getId()));
		namedParameters.put("name", dsg.getname());
		namedParameters.put("Desig", dsg.getDesig());
		namedParameterJdbcTemplate.update(query, namedParameters);
	}

	@SuppressWarnings("unchecked")
	public Designation selectDesignation(int Id) {
		String query = "SELECT * FROM DESIGNATION WHERE ID=:Id";
		SqlParameterSource namedParameters = new MapSqlParameterSource("Id",
				Integer.valueOf(Id));

		return (Designation) namedParameterJdbcTemplate.queryForObject(query,
				namedParameters, new RowMapper() {
		public Object mapRow(ResultSet resultSet, int rowNum)
				throws SQLException {
		return new Designation(resultSet.getInt("ID"),
				resultSet.getString("NAME"), resultSet
				.getString("DESIGNATION"));
			}
		});
	}

}

DesignationMain.java

package net.roseindia;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import net.roseindia.Designation;

public class DesignationMain {

	public static void main(String[] args) {
	ApplicationContext context = new ClassPathXmlApplicationContext(
				"NPJTemplate.xml");
	DesignationDAO designationDAO = (DesignationDAO) context
				.getBean("designationDAO");
	Designation myBean = new Designation(1, "Kapil Singh",
				"Java Programmer");
	designationDAO.insertDesignation(myBean);
	System.out.println(designationDAO.selectDesignation(1));
	}

}

NPJTemplate.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<bean id="NPJT" class="net.roseindia.NPJTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="designationDAO" class="net.roseindia.DesignationQuery">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://192.168.10.13:3306/ankdb" />
<property name="user" value="root" />
<property name="password" value="root" />
</bean>
</beans>

OUTPUT

After execution of code the DESIGNATION table :



The eclipse console :



Download Source Code
Advertisement

Related Tags for NamedParameterJdbcTemplate RowMapper:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 23, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com