Followers

springdao : jdbcTemplate(PreparedStatement)

Employee.java
=============

package springDAO;

public class Employee {
    private int id;
    private String name;
    private float salary;
    
    public Employee(){}
    public Employee(int id,String name,float salary){
    this.id = id;
    this.name = name;
    this.salary = salary;
    }

    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 float getSalary() {
        return salary;
    }

    public void setSalary(float salary) {
        this.salary = salary;
    }
   }

   Beans.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:p="http://www.springframework.org/schema/p"  
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> 
    
    <bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver" />  
        <property name="url" value="jdbc:derby://localhost:1527/batch" />  
        <property name="username" value="app" />  
        <property name="password" value="app" /> 
    
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
    <property name="dataSource" ref="ds"></property>  
    </bean>  
  
    <bean id="empdao" class="springDAO.EmployeeDAO">  
    <property name="jdbcTemplate" ref="jdbcTemplate"></property>  
    </bean>   
    
</beans>

EmployeeDAO.java
================

package springDAO;
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 EmployeeDAO {
    private JdbcTemplate jdbcTemplate;  
  
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {  
    this.jdbcTemplate = jdbcTemplate;  

public Boolean deleteEmployee(final Employee e){  
    String query = "delete from BATCH1 where id = ?";
    return jdbcTemplate.execute(query,new PreparedStatementCallback<Boolean>(){  
    @Override  
    public Boolean doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {                
        ps.setInt(1,e.getId()); 
        boolean count = ps.execute();
        return count;
    }    
    });  
}
public Boolean updateEmployee(final Employee e){  
    String query = "update BATCH1 set salary = ? where id = ?";
    return jdbcTemplate.execute(query,new PreparedStatementCallback<Boolean>(){  
    @Override  
    public Boolean doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {  
              
        ps.setInt(2,e.getId());  
        ps.setFloat(1,e.getSalary());  
        boolean count = ps.execute();
        return count;
    }    
    });  
}
public Boolean saveEmployee(final Employee e){  
    String query="insert into BATCH1 values(?,?,?)";  
    return jdbcTemplate.execute(query,new PreparedStatementCallback<Boolean>(){  
    @Override  
    public Boolean doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException {  
              
        ps.setInt(1,e.getId());  
        ps.setString(2,e.getName());  
        ps.setFloat(3,e.getSalary());  
        boolean count = ps.execute();
        return count;
    }    
    });  
}
}


Test.java
=========

package springDAO;
import org.springframework.context.ApplicationContext;  
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {

    public static void main(String[] args) {
        
        ApplicationContext ctx = new ClassPathXmlApplicationContext("Beans.xml");
        EmployeeDAO empDAO = ctx.getBean("empdao", EmployeeDAO.class);
        Employee emp = new Employee();
        emp.setId(9009);
        emp.setName("Sachin");
        emp.setSalary(30000f);
        empDAO.saveEmployeeByPreparedStatement(emp);
        System.out.println("Saved data successfully...");
    }    
}

No comments:

Post a Comment