Prepared Statement using Spring jdbcTemplate (Spring + MySQL)
Here is the code about how to use PreparedStatement in Spring using JDBCTemplate. This code is tested in Netbeans.

Student.java

package jclass;
public class Student {
   String sname,sid,sdept; 
   
   public Student(String sname, String sid, String sdept)
   {
       this.sname=sname;
       this.sid=sid;
       this.sdept=sdept;
   }
   
   public Student()
   {
       
   }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSid() {
        return sid;
    }

    public void setSid(String sid) {
        this.sid = sid;
    }

    public String getSdept() {
        return sdept;
    }

    public void setSdept(String sdept) {
        this.sdept = sdept;
    }
}

Studentdao.java

package jclass;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCallback;

public class Studentdao {
    
    JdbcTemplate jdbctemplate;

    public JdbcTemplate getJdbctemplate() {
        return jdbctemplate;
    }

    public void setJdbctemplate(JdbcTemplate jdbctemplate) {
        this.jdbctemplate = jdbctemplate;
    }
    
    public Boolean savestudent(final Student s)
    {
        String insq="insert into student values(?,?,?)";
        return jdbctemplate.execute(insq,new PreparedStatementCallback <Boolean>()
        {
            @Override
            public Boolean doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {
                ps.setString(1,s.getSname());
                ps.setString(2, s.getSid());
                ps.setString(3, s.getSdept());
                return ps.execute();
            }  
        });
    }
}

applicationContext.xml

<bean id="datasource1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">  
<property name="driverClassName" value="com.mysql.jdbc.Driver" />  
<property name="url" value="url" />  
<property name="username" value="" />  
<property name="password" value=" " />  
</bean>  
  
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
<property name="dataSource" ref="datasource1"></property>  
</bean>  
  
<bean id="studentdao" class="jclass.Studentdao">  
<property name="jdbctemplate" ref="jdbcTemplate"></property>  
</bean>

statementexec.java

package jclass;

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

public class statementexec {
    public static void main(String args[])
    {
        ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
        Studentdao dao=(Studentdao)ac.getBean("studentdao");
        dao.savestudent(new Student("Bobby","007","Assassin"));  
    } 
}

 

By Sri

Leave a Reply

Your email address will not be published.