一、VO类
1 package cn.mldn.lxh.vo; 2 3 import java.util.Date; 4 5 public class Emp { 6 private int empno; 7 private String ename; 8 private String job; 9 private Date hiredate; 10 private float sal; 11 public int getEmpno() { 12 return empno; 13 } 14 public void setEmpno(int empno) { 15 this.empno = empno; 16 } 17 public String getEname() { 18 return ename; 19 } 20 public void setEname(String ename) { 21 this.ename = ename; 22 } 23 public String getJob() { 24 return job; 25 } 26 public void setJob(String job) { 27 this.job = job; 28 } 29 public Date getHiredate() { 30 return hiredate; 31 } 32 public void setHiredate(Date hiredate) { 33 this.hiredate = hiredate; 34 } 35 public float getSal() { 36 return sal; 37 } 38 public void setSal(float sal) { 39 this.sal = sal; 40 } 41 42 43 }
二、数据库连接类
1 package cn.mldn.lxh.dbc; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 6 public class DatabaseConnection { 7 private static final String DBDRIVER = "com.mysql.jdbc.Driver"; 8 private static final String DBURL = "jdbc:mysql://localhost:3306/mldn"; 9 private static final String DBUSER = "root"; 10 private static final String DBPASS = "xyzk123"; 11 private Connection conn = null; 12 public DatabaseConnection() throws Exception{ 13 try{ 14 Class.forName(DBDRIVER); 15 this.conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS); 16 }catch(Exception e){ 17 throw e; 18 } 19 } 20 public Connection getConnection(){ 21 return this.conn; 22 } 23 public void close()throws Exception{ 24 if(this.conn != null){ 25 try{ 26 this.conn.close(); 27 }catch(Exception e){ 28 throw e; 29 } 30 } 31 } 32 }
三、DAO操作接口(标准)
1 package cn.mldn.lxh.dao; 2 3 import java.util.List; 4 5 import cn.mldn.lxh.vo.Emp; 6 7 public interface IEmpDAO { 8 9 public boolean doCreate(Emp emp) throws Exception; //数据增加操作 10 11 public List<Emp> findAll(String keyWord) throws Exception; //查询全部数据 12 13 public Emp findById(int empno) throws Exception; //根据雇员编号查询雇员信息 14 }
四、DAO接口的真实主题实现类
1 package cn.mldn.lxh.dao.impl; 2 3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.util.ArrayList; 7 import java.util.List; 8 9 import cn.mldn.lxh.dao.IEmpDAO; 10 import cn.mldn.lxh.vo.Emp; 11 12 public class EmpDAOImpl implements IEmpDAO { 13 private Connection conn = null; 14 private PreparedStatement pstmt = null; 15 public EmpDAOImpl(Connection conn){ 16 this.conn = conn; 17 } 18 19 public boolean doCreate(Emp emp) throws Exception { 20 boolean flag = false; 21 String sql = "INSERT INTO emp(empno,ename,job,hiredate,sal) VALUES(?,?,?,?,?)"; 22 this.pstmt = this.conn.prepareStatement(sql); 23 this.pstmt.setInt(1, emp.getEmpno()); 24 this.pstmt.setString(2, emp.getEname()); 25 this.pstmt.setString(3, emp.getJob()); 26 this.pstmt.setDate(4, new java.sql.Date(emp.getHiredate().getTime())); 27 this.pstmt.setFloat(5, emp.getSal()); 28 if(this.pstmt.executeUpdate()>0){ 29 flag = true; 30 } 31 this.pstmt.close(); 32 return flag; 33 } 34 35 public List<Emp> findAll(String keyWord) throws Exception { 36 List<Emp> all = new ArrayList<Emp>(); 37 String sql = "SELECT empno,ename,job,hiredate,sal FROM emp WHERE ename LIKE ? OR job LIKE?"; 38 this.pstmt = this.conn.prepareStatement(sql); 39 this.pstmt.setString(1, "%" + keyWord + "%"); 40 this.pstmt.setString(2, "%" + keyWord + "%"); 41 ResultSet rs = this.pstmt.executeQuery(); 42 Emp emp = null; 43 while(rs.next()){ 44 emp = new Emp(); 45 emp.setEmpno(rs.getInt(1)); 46 emp.setEname(rs.getString(2)); 47 emp.setJob(rs.getString(3)); 48 emp.setHiredate(rs.getDate(4)); 49 emp.setSal(rs.getFloat(5)); 50 all.add(emp); 51 } 52 this.pstmt.close(); 53 return all; 54 } 55 56 public Emp findById(int empno) throws Exception { 57 Emp emp = null; 58 String sql = "SELECT empno,ename,job,hiredate,sal FROM emp WHERE empno = ?"; 59 this.pstmt = this.conn.prepareStatement(sql); 60 this.pstmt.setInt(1, empno); 61 ResultSet rs = this.pstmt.executeQuery(); 62 if(rs.next()){ 63 emp = new Emp(); 64 emp.setEmpno(rs.getInt(1)); 65 emp.setEname(rs.getString(2)); 66 emp.setJob(rs.getString(3)); 67 emp.setHiredate(rs.getDate(4)); 68 emp.setSal(rs.getFloat(5)); 69 } 70 this.pstmt.close(); 71 return emp; 72 } 73 }
五、代理实现类
1 package cn.mldn.lxh.dao.proxy; 2 3 import java.util.List; 4 5 import cn.mldn.lxh.dao.IEmpDAO; 6 import cn.mldn.lxh.dao.impl.EmpDAOImpl; 7 import cn.mldn.lxh.dbc.DatabaseConnection; 8 import cn.mldn.lxh.vo.Emp; 9 10 public class EmpDAOProxy implements IEmpDAO{ 11 private DatabaseConnection dbc = null; 12 private IEmpDAO dao = null; 13 public EmpDAOProxy() throws Exception{ 14 this.dbc = new DatabaseConnection(); 15 this.dao = new EmpDAOImpl(this.dbc.getConnection()); 16 } 17 public boolean doCreate(Emp emp) throws Exception{ 18 boolean flag = false; 19 try{ 20 if(this.dao.findById(emp.getEmpno()) == null){ 21 flag = this.dao.doCreate(emp); 22 } 23 }catch(Exception e){ 24 throw e; 25 }finally{ 26 this.dbc.close(); 27 } 28 return flag; 29 } 30 public List<Emp> findAll(String keyWord) throws Exception { 31 List<Emp> all = null; 32 try{ 33 all = this.dao.findAll(keyWord); 34 }catch(Exception e){ 35 throw e; 36 }finally{ 37 this.dbc.close(); 38 } 39 return all; 40 } 41 public Emp findById(int empno) throws Exception { 42 Emp emp = null; 43 try{ 44 emp = this.dao.findById(empno); 45 }catch(Exception e){ 46 throw e; 47 }finally{ 48 this.dbc.close(); 49 } 50 return emp; 51 } 52 }
六、DAO工厂类
1 package cn.mldn.lxh.factory; 2 3 import cn.mldn.lxh.dao.IEmpDAO; 4 import cn.mldn.lxh.dao.proxy.EmpDAOProxy; 5 6 public class DAOFactory { 7 public static IEmpDAO getIEmpDAOInstance()throws Exception{ 8 return new EmpDAOProxy(); 9 } 10 }
七、简单测试
(1)
1 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 4 <title>Dao</title> 5 </head> 6 7 <body> 8 <form action="emp_insert_do.jsp" method="post"> 9 编号:<input type="text" name="empno" /><br /> 10 姓名:<input type="text" name="ename" /><br /> 11 职位:<input type="text" name="job" /><br /> 12 日期:<input type="text" name="hiredate" /><br /> 13 工资:<input type="text" name="sal" /><br /> 14 <input type="submit" value="注册" /> 15 <input type="reset" value="重置" /> 16 </form> 17 </body> 18 </html>
(2)
1 <html xmlns="http://www.w3.org/1999/xhtml"> 2 <head> 3 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 4 <title>peixun</title> 5 </head> 6 7 <body> 8 <% 9 Emp emp = new Emp(); 10 emp.setEmpno(Integer.parseInt(request.getParameter("empno"))); 11 emp.setEname(request.getParameter("ename")); 12 emp.setJob(request.getParameter("job")); 13 14 emp.setHiredate(new SimpleDateFormat("yyyy-MM-dd").parse(request 15 .getParameter("hiredate"))); 16 17 emp.setSal(Float.parseFloat(request.getParameter("sal"))); 18 try{ 19 if(DAOFactory.getIEmpDAOInstance().doCreate(emp)){ 20 %> 21 <h3>信息添加成功</h3> 22 <% 23 }else{ 24 %> 25 <h3>信息添加失败</h3> 26 <% 27 } 28 29 }catch(Exception e){ 30 e.printStackTrace(); 31 } 32 %> 33 </body> 34 </html>