spring jdbc sample
DESCRIPTION
Spring to JDB SamplesTRANSCRIPT
Spring -JDBC Template Sample Code:Spring-JDBC:
Automatic connection handling.
Template Based programming
Multiple Data source availability
Built-in Methods for SQL operations.
RowMapper for Multi object Operations.
Config.xml:
Employee.java
package com.app;public class Employee {
private Integer empId;
private String empName;
private Double empSal;
public Employee() {
super();
}
public Employee(Integer empId, String empName, Double empSal) {
super();
this.empId = empId;
this.empName = empName;
this.empSal = empSal;
}
public Integer getEmpId() {
return empId;
}
public void setEmpId(Integer empId) {
this.empId = empId;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public Double getEmpSal() {
return empSal;
}
public void setEmpSal(Double empSal) {
this.empSal = empSal;
}
@Override
public String toString() {
return "Employee [empId=" + empId + ", empName=" + empName
+ ", empSal=" + empSal + "]";
}}package com.app;public interface IEmployeeDao {
public void createEmployee(Employee emp);}
package com.app;import org.springframework.jdbc.core.JdbcTemplate;public class EmployeeDaoImpl implements IEmployeeDao {
private JdbcTemplate template;
public void setTemplate(JdbcTemplate template) {
this.template = template;
}
@Override
public void createEmployee(Employee emp) {
String sql="insert into employee values(?,?,?)";
template.update(sql,emp.getEmpId(),emp.getEmpName(),emp.getEmpSal());
}}Main.java
package com.app;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {
public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("config.xml");
IEmployeeDao dao=(IEmployeeDao)context.getBean("empDao");
Employee emp=new Employee();
emp.setEmpId(101);
emp.setEmpName("abcd");
emp.setEmpSal(200.36);
dao.createEmployee(emp);
}}