==============
package springDAO;
public class Employee {
private int id;
private String name;
private float salary;
private String address;
public Employee(){}
public Employee(int id,String name,float salary,String address){
this.id = id;
this.name = name;
this.salary = salary;
this.address = address;
}
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;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
EmployeeDAO.java
=================
package springDAO;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository(value = "empdao")
public class EmployeeDAO {
private JdbcTemplate jdbcTemplate;
private DataSource ds;
@Autowired
public void setDs(DataSource ds) {
System.out.println("Hi");
this.jdbcTemplate = new JdbcTemplate(ds);
}
public int saveEmployee(Employee emp){
String query = "insert into MYEMP values("+emp.getId()+",'"+emp.getName()+"',"+emp.getSalary()+",'"+emp.getAddress()+"')";
return jdbcTemplate.update(query);
}
public int updateEmployee(Employee emp){
String query = "update myemp set name = '"+emp.getName()+"' , salary ="+emp.getSalary()+" , address = '"+emp.getAddress()+"' where id = "+emp.getId();
return jdbcTemplate.update(query);
}
public int deleteEmployee(Employee emp){
String query="delete from employee where id="+emp.getId();
return jdbcTemplate.update(query);
}
}
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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="springDAO" />
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />
<bean id="dataSource" 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>
</beans>
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(9888);
emp.setName("Mohan33");
emp.setSalary(320f);
emp.setAddress("BLR");
int status = empDAO.saveEmployee(emp);
// int status = empDAO.updateEmployee(emp);
//int status = empDAO.deleteEmployee(emp);
System.out.println("==> "+status);
}
}
No comments:
Post a Comment