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 org.springframework.jdbc.core.JdbcTemplate;
public class EmployeeDAO {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int saveEmployee(Employee e){
String query="insert into employee values("+e.getId()+",'"+e.getName()+"',"+e.getSalary()+")";
return jdbcTemplate.update(query);
}
public int updateEmployee(Employee e){
String query="update employee set name='"+e.getName()+"',salary="+e.getSalary()+" where id="+e.getId();
return jdbcTemplate.update(query);
}
public int deleteEmployee(Employee e){
String query="delete from employee where id="+e.getId();
return jdbcTemplate.update(query);
}
}
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(4744);
emp.setName("MOhan33");
emp.setSalary(320f);
int status = empDAO.saveEmployee(emp);
//int status = empDAO.updateEmployee(emp);
// int status = empDAO.deleteEmployee(emp);
System.out.println("==> "+status);
}
}
=============
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 org.springframework.jdbc.core.JdbcTemplate;
public class EmployeeDAO {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int saveEmployee(Employee e){
String query="insert into employee values("+e.getId()+",'"+e.getName()+"',"+e.getSalary()+")";
return jdbcTemplate.update(query);
}
public int updateEmployee(Employee e){
String query="update employee set name='"+e.getName()+"',salary="+e.getSalary()+" where id="+e.getId();
return jdbcTemplate.update(query);
}
public int deleteEmployee(Employee e){
String query="delete from employee where id="+e.getId();
return jdbcTemplate.update(query);
}
}
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(4744);
emp.setName("MOhan33");
emp.setSalary(320f);
int status = empDAO.saveEmployee(emp);
//int status = empDAO.updateEmployee(emp);
// int status = empDAO.deleteEmployee(emp);
System.out.println("==> "+status);
}
}
No comments:
Post a Comment