23.NamedParameterJdbcTemplate

NamedParameterJdbcTemplate
·         Similar to PreparedStatement
·         We use “: names”  instead of ?
INSERT INTO student VALUES (:sno,:name,:address)
·         we have a method pubic T execute(String sql,Map map,PreparedStatementCallback psc)  
·         We have to implement that method.
Example:
//StudentBo.java
package bo;

public class StudentBo {
private int sno;
private String name;
private String address;
public int getSno() {
       return sno;
}
public void setSno(int sno) {
       this.sno = sno;
}
public String getName() {
       return name;
}
public void setName(String name) {
       this.name = name;
}
public String getAddress() {
       return address;
}
public void setAddress(String address) {
       this.address = address;
}

}

package dao;

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

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.PreparedStatementCallback;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

import bo.StudentBo;

public class NamedParameterJdbcTemplateDao {
      
       private NamedParameterJdbcTemplate template;

       public NamedParameterJdbcTemplate getTemplate() {
              return template;
       }

       public void setTemplate(NamedParameterJdbcTemplate template) {
              this.template = template;
       }
      
      
       public int save(StudentBo bo){
             
              String qry ="INSERT INTO student VALUES (:sno,:name,:address)";
              Map<String, Object> map = new HashMap<String, Object>();
              map.put("sno", bo.getSno());
              map.put("name", bo.getName());
              map.put("address", bo.getAddress());
             
       Object ob =    template.execute(qry, map, new PreparedStatementCallback<Object>() {
               @Override
              public Object doInPreparedStatement(PreparedStatement ps)
                           throws SQLException, DataAccessException {                   
                     return ps.executeUpdate();
              }
              }
                    
              );
                          
              System.out.println("Saved--->"+ob);
      
              return 1;
       }

}

//Main.java
package main;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import bo.StudentBo;
import dao.NamedParameterJdbcTemplateDao;


public class Main {
       public static void main(String[] args) {
               
              BeanFactory factory = new ClassPathXmlApplicationContext("res/s.xml");
              NamedParameterJdbcTemplateDao dao = (NamedParameterJdbcTemplateDao) factory.getBean("dao");
              StudentBo bo = new StudentBo();
              bo.setSno(5);
              bo.setName("VINOD");
              bo.setAddress("BANGLORE");
              System.out.println("SAVING : "+dao.save(bo));
       }
}

<beans>
                <!-- 1. set  Properties to  DriverManagerDataSource  -->
                     <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
                           <property name="url" value="jdbc:mysql://localhost:3306/test" />
                           <property name="username" value="root" />
                           <property name="password" value="infy" />
                     </bean>
               
                 <!-- 2.set JdbcTemplate  Properties -->     
                     <bean id="template" class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">
                            <property name="dataSource" ref="ds"></property>                           
                     </bean>
                    
                     <!-- 3.set DAO  Properties -->
                <bean id="dao" class="dao.NamedParameterJdbcTemplateDao">
                            <property name="template" ref="template"></property>                       
                     </bean>
                </beans>