=============
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.namedparam.NamedParameterJdbcTemplate">
<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 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;
public class EmployeeDAO {
private NamedParameterJdbcTemplate template;
public EmployeeDAO(NamedParameterJdbcTemplate template) {
this.template = template;
}
public void update(Employee e){
String query="update MYEMP set name= :name where id=:id";
Map<String,Object> map=new HashMap<String,Object>();
map.put("id",e.getId());
map.put("name",e.getName());
//map.put("salary",e.getSalary());
template.execute(query,map,new PreparedStatementCallback() {
@Override
public Object doInPreparedStatement(PreparedStatement ps)
throws SQLException, DataAccessException {
return ps.executeUpdate();
}
});
}
public void save(Employee e){
String query="insert into MYEMP values (:id,:name,:salary)";
Map<String,Object> map=new HashMap<String,Object>();
map.put("id",e.getId());
map.put("name",e.getName());
map.put("salary",e.getSalary());
template.execute(query,map,new PreparedStatementCallback() {
@Override
public Object doInPreparedStatement(PreparedStatement ps)
throws SQLException, DataAccessException {
return ps.executeUpdate();
}
});
}
public void delete(Employee e){
String query="delete from MYEMP where id = :id";
Map<String,Object> map=new HashMap<String,Object>();
map.put("id",e.getId());
template.execute(query,map,new PreparedStatementCallback() {
@Override
public Object doInPreparedStatement(PreparedStatement ps)
throws SQLException, DataAccessException {
return ps.executeUpdate();
}
});
}
}
Test.java
=========
package springDAO;
import java.util.List;
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(9);
emp.setName("SSSS");
emp.setSalary(320f);
// empDAO.delete(emp);
empDAO.update(emp);
}
}
No comments:
Post a Comment