<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <beans> <bean id="DatabaseConnection" class="com.hz.dao.DatabaseConnection"> <property name="url"> <value>jdbc:oracle:thin:@localhost:1521:orcldb</value> </property> <property name="username"> <value>scott</value> </property> <property name="password"> <value>tiger</value> </property> </bean> <bean id="EmployeeDAO" class="com.hz.dao.EmployeeDAO"> <property name="dc"> <ref bean="DatabaseConnection"/> </property> </bean> <bean id="EmployeeService" class="com.hz.service.EmployeeService"> <property name="dao"> <ref bean="EmployeeDAO"/> </property> </bean> </beans>
package com.hz.dao; import java.sql.*; public class DatabaseConnection { private String url; private String username; private String password; public void setUrl(String url) { this.url = url; } public void setUsername(String username) { this.username = username; } public void setPassword(String password) { this.password = password; } static{ try { Class.forName("oracle.jdbc.driver.OracleDriver"); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public Connection getConnection(){ Connection conn = null; try { conn = DriverManager.getConnection(url, username, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } }
package com.howin.dao; public class Employee { private String empno; private String ename; public String getEmpno() { return empno; } public void setEmpno(String empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } }
package com.hz.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.List; public class EmployeeDAO { private DatabaseConnection dc; public void setDc(DatabaseConnection dc) { this.dc = dc; } public void addEmp(Employee emp){ Connection conn = null; PreparedStatement ps = null; String sql = "INSERT INTO EMPLOYEE (EMPNO, ENAME) VALUES (?,?)"; conn = dc.getConnection(); try { ps = conn.prepareStatement(sql); ps.setString(1, emp.getEmpno()); ps.setString(2, emp.getEname()); ps.execute(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally{ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public List<Employee> query(){ return null; } }
package com.hz.dao; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import com.hz.service.EmployeeService; public class Test { public static void main(String[] args) { //1. 加载配置文件, 获取Spring容器 ApplicationContext ac = new FileSystemXmlApplicationContext("src/applicationContext.xml"); EmployeeService es = (EmployeeService) ac.getBean("EmployeeService"); Employee emp = new Employee(); emp.setEmpno("3"); emp.setEname("Tom"); es.addEmployee(emp); } }
需要引入包 spring.jar commons-logging.jar Oracle驱动