Followers

springdao : SimpleJdbcTemplate

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/MYEMP" />  
        <property name="username" value="app" />  
        <property name="password" value="app" /> 
    
    </bean>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.simple.SimpleJdbcTemplate">  
    <constructor-arg ref="ds"></constructor-arg> 
    </bean>  
  
    <bean id="empdao" class="springDAO.EmployeeDAO">  
    <constructor-arg><ref bean="jdbcTemplate"/></constructor-arg>
    </bean>  
</beans>



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

package springDAO;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;

public class EmployeeDAO {
    SimpleJdbcTemplate template;  
  
public EmployeeDAO(SimpleJdbcTemplate template) {  
        this.template = template;  

public int save(Employee e){  
String query="insert into MYEMP values(?,?,?)";  
int count = template.update(query,e.getId(),e.getName(),e.getSalary());
return count;    
}
public int update (Employee e){  
String query="update MYEMP set name=? where id=?";  
int count = template.update(query,e.getName(),e.getId());
return count;  
  
//String query="update employee set name=?,salary=? where id=?";  
//int count = template.update(query,e.getName(),e.getSalary(),e.getId());  
//return count;
}
public int delete(Employee e){  
String query="delete MYEMP where id=?";  
int count = template.update(query,e.getId());
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(4);
        emp.setName("Mahesh");
        emp.setSalary(320f);
       int count = empDAO.delete(emp);
       //int count =  empDAO.save(emp);        
       
    }
}

No comments:

Post a Comment