DAO设计模式例子

 1 <!-- 输入界面 -->
 2 <%@page contentType="text/html" pageEncoding="GBK"%>
 3 <html>
 4   <head>
 5   </head>
 6   <% request.setCharacterEncoding("GBK"); //解决乱码问题 %>    
 7 
 8   
 9   <body> 
10     <form action="emp_insert_do.jsp" method="post">
11     empno:<input type="text" name="empno"><br>
12 
13     ename:<input type="text" name="ename"><br>
14 
15      job: <input type="text" name="job"><br>
16      
17      hiredate: <input type="text" name="hiredate"><br>
18 
19     sal: <input type="text" name="sal"><br>
20     
21     
22     <input type="submit" value = "submit">
23     <input type="reset" value = "reset">
24     </form>
25   </body>
26 </html>
 1 <!-- 后台插入数据 -->
 2 <%@page import="info.haowei.Dao.factory.DAOFactory"%>
 3 <%@page import="java.text.SimpleDateFormat"%>
 4 <%@page import="info.haowei.Dao.vo.Emp"%>
 5 <%@page contentType="text/html" pageEncoding="GBK"%>
 6 <html>
 7   <head>
 8   </head>
 9   <% request.setCharacterEncoding("GBK"); //解决乱码问题 %>    
10 
11   
12   <body> 
13     <%
14         Emp emp = new Emp();
15         emp.setEmpno(Integer.parseInt(request.getParameter("empno")));
16         emp.setEname(request.getParameter("ename"));
17         emp.setJob(request.getParameter("job"));
18         
19         emp.setHiredate((new SimpleDateFormat("yyyy-MM-dd")).parse(request.getParameter("hiredate")));
20         emp.setSal(Float.parseFloat(request.getParameter("sal")));
21         
22         try{
23         
24         if(DAOFactory.getIEmpDAOInstance().doCreate(emp)){
25         
26         
27         
28         %>
29         
30         <h3>添加成功!!</h3>
31      
32      <%
33      }else{
34      %>
35      <h3>添加失败!!!</h3>
36      <%
37      }
38      }catch(Exception e){
39      e.printStackTrace();
40      }
41       %>
42      
43   </body>
44 </html>
 1 //vo类
 2 package info.haowei.Dao.vo;
 3 
 4 import java.util.Date;
 5 
 6 public class Emp {
 7 
 8     private int empno;
 9     private String ename;
10     private String job;
11     private Date hiredate;
12     private float sal;
13     
14     public int getEmpno() {
15         return empno;
16     }
17     public void setEmpno(int empno) {
18         this.empno = empno;
19     }
20     public String getEname() {
21         return ename;
22     }
23     public void setEname(String ename) {
24         this.ename = ename;
25     }
26     public String getJob() {
27         return job;
28     }
29     public void setJob(String job) {
30         this.job = job;
31     }
32     public Date getHiredate() {
33         return hiredate;
34     }
35     public void setHiredate(Date hiredate) {
36         this.hiredate = hiredate;
37     }
38     public float getSal() {
39         return sal;
40     }
41     public void setSal(float sal) {
42         this.sal = sal;
43     }
44     
45 }
 1 //工厂类
 2 package info.haowei.Dao.factory;
 3 
 4 import info.haowei.Dao.dao.IEmpDAO;
 5 import info.haowei.Dao.dao.proxy.EmpDAOProxy;
 6 
 7 public class DAOFactory {
 8 
 9     public static IEmpDAO getIEmpDAOInstance(){
10         return new EmpDAOProxy();
11     }
12 }
 1 //操作数据库的类
 2 package info.haowei.Dao.dbc;
 3 
 4 import java.sql.Connection;
 5 import java.sql.DriverManager;
 6 import java.sql.SQLException;
 7 
 8 public class DatabaseConnection {
 9 
10     private static final String DBDRIVER = "org.gjt.mm.mysql.Driver";
11     public static final String DBURL = "jdbc:mysql://localhost:3306/zhw";
12     public static final String DBUSER = "root";
13     public static final String DBPASS = "root";
14     private Connection conn;
15 
16     public DatabaseConnection() {
17         try {
18             Class.forName(DBDRIVER);
19             this.conn = DriverManager.getConnection(DBURL, DBUSER, DBPASS);
20         } catch (ClassNotFoundException e) {
21             e.printStackTrace();
22         } catch (SQLException e) {
23             e.printStackTrace();
24         }
25     }
26 
27     public Connection getConnection() {
28         return this.conn;
29     }
30 
31     public void close() {
32         if (this.conn != null) {
33             try {
34                 this.conn.close();
35             } catch (SQLException e) {
36                 e.printStackTrace();
37             }
38         }
39     }
40 
41 }
 1 //代理类.相当于回调吧
 2 package info.haowei.Dao.dao.proxy;
 3 
 4 import java.util.List;
 5 
 6 import info.haowei.Dao.dao.IEmpDAO;
 7 import info.haowei.Dao.dao.impl.EmpDAOImpl;
 8 import info.haowei.Dao.dbc.DatabaseConnection;
 9 import info.haowei.Dao.vo.Emp;
10 
11 //代理
12 public class EmpDAOProxy implements IEmpDAO {
13 
14     private DatabaseConnection dbc = null;
15     private IEmpDAO dao = null;
16 
17     public EmpDAOProxy() {
18         this.dbc = new DatabaseConnection();
19         this.dao = new EmpDAOImpl(this.dbc.getConnection());
20     }
21 
22     public boolean doCreate(Emp emp) {
23         boolean flag = false;
24         try {
25             if(this.dao.findById(emp.getEmpno())==null){
26                 flag = this.dao.doCreate(emp);
27             }
28             
29         } catch (Exception e) {
30 
31         } finally {
32             dbc.close();
33         }
34 
35         return flag;
36     }
37 
38     public List<Emp> findAll(String keyword) {
39         List<Emp>all  = null;
40         try {
41             all = this.dao.findAll(keyword);
42         } catch (Exception e) {
43 
44         } finally {
45             dbc.close();
46         }
47         return all;
48     }
49 
50     public Emp findById(int empno) {
51         Emp emp = null;
52         try {
53             emp = this.dao.findById(empno);
54         } catch (Exception e) {
55 
56         } finally {
57             dbc.close();
58         }
59         return emp;
60     }
61 }
  1 //接口实现类,具体操作封装类
  2 package info.haowei.Dao.dao.impl;
  3 
  4 import java.sql.Connection;
  5 import java.sql.PreparedStatement;
  6 import java.sql.ResultSet;
  7 import java.sql.SQLException;
  8 import java.util.ArrayList;
  9 import java.util.List;
 10 
 11 import javax.swing.text.html.HTMLDocument.HTMLReader.PreAction;
 12 
 13 import info.haowei.Dao.dao.IEmpDAO;
 14 import info.haowei.Dao.vo.Emp;
 15 
 16 public class EmpDAOImpl implements IEmpDAO{
 17 
 18     
 19     private Connection conn = null;
 20     private PreparedStatement pstmt = null;
 21     public EmpDAOImpl(Connection conn){
 22         this.conn = conn;
 23     }
 24     
 25     
 26     
 27     
 28     
 29     
 30     public boolean doCreate(Emp emp) {
 31         
 32         boolean flag = false;
 33         String sql = "Insert into emp (empno,ename,job,hiredate,sal)Values(?,?,?,?,?)";
 34         
 35         try {
 36             this.pstmt = this.conn.prepareStatement(sql);
 37             this.pstmt.setInt(1,emp.getEmpno());
 38             this.pstmt.setString(2, emp.getEname());
 39             this.pstmt.setString(3, emp.getJob());
 40             this.pstmt.setDate(4, new java.sql.Date(emp.getHiredate().getTime()));
 41             this.pstmt.setFloat(5, emp.getSal());
 42             if(this.pstmt.executeUpdate()>0){//如果存在更新
 43                 flag = true;
 44             }
 45             this.pstmt.close();
 46         } catch (SQLException e) {
 47             e.printStackTrace();
 48         }
 49         return flag;
 50     }
 51 
 52     public List<Emp> findAll(String keyword) {
 53         List<Emp>all = new ArrayList<Emp>();
 54         String sql = "select empno,ename,job,hiredate,sal from emp where ename like ? or job like ?";
 55         try {
 56             this.pstmt = this.conn.prepareStatement(sql);
 57             this.pstmt.setString(1,"%"+keyword+"%");
 58             this.pstmt.setString(2,"%"+keyword+"%");
 59             ResultSet rs = this.pstmt.executeQuery();
 60             Emp emp = null;
 61             while(rs.next()){
 62                 emp = new Emp();
 63                 emp.setEmpno(rs.getInt(1));
 64                 emp.setEname(rs.getString(2));
 65                 emp.setJob(rs.getString(3));
 66                 emp.setHiredate(rs.getDate(4));
 67                 emp.setSal(rs.getFloat(5));
 68                 all.add(emp);
 69             }
 70             
 71             this.pstmt.close();
 72         } catch (SQLException e) {
 73             e.printStackTrace();
 74         }
 75         return all;
 76         
 77     
 78     }
 79 
 80     public Emp findById(int empno) {
 81         Emp emp = null;
 82         String sql = "select empno,ename,job,hiredate,sal from emp where empno=?";
 83         try {
 84             this.pstmt = this.conn.prepareStatement(sql);
 85             this.pstmt.setInt(1, empno);
 86             ResultSet rs =  this.pstmt.executeQuery();
 87             if(rs.next()){
 88                 emp = new Emp();
 89                 emp.setEmpno(rs.getInt(1));
 90                 emp.setEname(rs.getString(2));
 91                 emp.setJob(rs.getString(3));
 92                 emp.setHiredate(rs.getDate(4));
 93                 emp.setSal(rs.getFloat(5));
 94             }
 95             this.pstmt.close();
 96         } catch (SQLException e) {
 97             e.printStackTrace();
 98         }
 99         return emp;
100     }
101     
102     
103 }
 1 //操作接口
 2 package info.haowei.Dao.dao;
 3 
 4 import java.util.List;
 5 
 6 import info.haowei.Dao.vo.Emp;
 7 
 8 public interface IEmpDAO {
 9     
10     public boolean doCreate(Emp emp);
11     public List<Emp>findAll(String keyword);
12     public Emp findById(int empno);
13 
14 }
 1 //主函数类
 2 
 3 package info.haowei.Dao.test;
 4 
 5 import java.util.Date;
 6 import java.util.Iterator;
 7 import java.util.List;
 8 
 9 import info.haowei.Dao.factory.DAOFactory;
10 import info.haowei.Dao.vo.Emp;
11 
12 public class DAOTest {
13     
14     public static void main(String[] args) {
15         
16         /*
17          * 插入
18         Emp emp = null;
19         for(int x = 0 ; x< 5; x++ ){
20             emp = new Emp();
21             emp.setEmpno(1000+x);
22             emp.setEname("zhw"+x);
23             emp.setJob("coding" +x);
24             emp.setHiredate(new Date());
25             emp.setSal(500+x);
26             DAOFactory.getIEmpDAOInstance().doCreate(emp);
27             
28         }
29         
30         */
31         
32         List<Emp>all = DAOFactory.getIEmpDAOInstance().findAll("");
33         Iterator<Emp>iter = all.iterator();
34         while(iter.hasNext()){
35             
36             Emp emp = iter.next();
37             System.out.println(emp.getEmpno()+"    "+emp.getEname());
38         }
39         
40         
41     }
42 
43 }
原文地址:https://www.cnblogs.com/elleniou/p/2699155.html