zoukankan      html  css  js  c++  java
  • 总结学了 j2ee 一月半后的项目情况(1)

    项目有三个,殊途同归:

    1、MODEL1【jsp+javabean/dao】 或者 MODEL2 【jsp+servlet+javabean/dao】

    2、三个项目所使用的模式分别是不同讲师所讲的,魔乐java、韩顺平j2ee 、java开放课程二期jsp,三者同时使用EL / JSTL 

    源码:【魔乐 雇员管理系统(MODEL1) 】

    1、定义IempDAO接口如下:

    1 package fafa.vo;
    2 import java.util.*;
    3 //前面的大写I 表示是接口
    4 public interface IempDAO {
    5     public boolean doCreate(Emp emp) throws Exception ;
    6     public List<Emp> findAll(String keyword) throws Exception ;
    7     public Emp findById(int empno) throws Exception ;
    8 }

    2、定义实现接口类 EmpDAOImpl 实现数据库CRUD

     1 package fafa.vo;
     2 import java.sql.*;
     3 import java.util.*;
     4 public class EmpDAOImpl implements IempDAO {
     5     private Connection conn = null ;
     6     private PreparedStatement pstmt = null ;
     7     
     8     public EmpDAOImpl (Connection conn){
     9         this.conn = conn ;
    10     }
    11     
    12     //创建
    13     public boolean doCreate(Emp emp) throws Exception {
    14         boolean flag = false ;
    15         String sql = "INSERT INTO emp(empno, ename, job, hiredate,sal) VALUES(?,?,?,?,?)" ;
    16         this.pstmt = this.conn.prepareStatement(sql) ; //sql预处理 
    17         
    18         //赋值
    19         this.pstmt.setInt(1, emp.getEmpno()) ;
    20         this.pstmt.setString(2, emp.getEname()) ;
    21         this.pstmt.setString(3, emp.getJob()) ;
    22         this.pstmt.setDate(4, new java.sql.Date(emp.getHirdate().getTime())) ;
    23         this.pstmt.setFloat(5, emp.getSal()) ;
    24         //执行更新判断
    25         if (this.pstmt.executeUpdate() > 0){
    26             flag = true ;
    27         }
    28         //关闭
    29         this.pstmt.close() ;
    30         return flag ;
    31     }
    32 
    33     //查询全部
    34     public List<Emp> findAll(String keyword) throws Exception {
    35         List<Emp> all = new ArrayList<Emp>() ;
    36         String sql = "SELECT empno,ename,job,hiredate,sal FROM emp WHERE ename LIKE ? OR job LIKE ?" ;
    37         this.pstmt = this.conn.prepareStatement(sql) ;
    38         
    39         this.pstmt.setString(1, "%"+keyword+"%") ;
    40         this.pstmt.setString(2, "%"+keyword+"%") ;
    41         
    42         ResultSet rs = this.pstmt.executeQuery() ;
    43         Emp emp = null ;
    44         while (rs.next()){
    45             emp = new Emp() ;
    46             emp.setEmpno(rs.getInt(1)) ;
    47             emp.setEname(rs.getString(2)) ;
    48             emp.setJob(rs.getString(3)) ;
    49             emp.setHirdate(rs.getDate(4)) ;
    50             emp.setSal(rs.getFloat(5)) ;
    51             all.add(emp) ;
    52         }
    53         this.pstmt.close() ;
    54         return all ;
    55     }
    56 
    57     //查询一个(ID)
    58     public Emp findById(int empno) throws Exception {
    59         Emp emp = null ;
    60         String sql = "SELECT empno,ename,job,hiredate,sal FROM emp WHERE empno = ?" ;
    61         
    62         this.pstmt = this.conn.prepareStatement(sql) ;    //sql预处理
    63         this.pstmt.setInt(1, empno) ;                    //设置值
    64         ResultSet rs = this.pstmt.executeQuery() ;        //执行sql
    65         if (rs.next()){
    66             emp = new Emp() ;
    67             emp.setEmpno(rs.getInt(1)) ;
    68             emp.setEname(rs.getString(2)) ;
    69             emp.setJob(rs.getString(3)) ;
    70             emp.setHirdate(rs.getDate(4)) ;
    71             emp.setSal(rs.getFloat(5)) ;
    72         }
    73         this.pstmt.close() ;
    74         return emp ;
    75     }
    76 }

    3 、定义代理类EmpDAOproxy 实现接口 并调用实际实现类完成操作

     1 package fafa.vo;
     2 
     3 import java.util.*;
     4 
     5 public class EmpDAOproxy implements IempDAO {
     6     private DatabaseConnection dbc = null ;
     7     private IempDAO dao = null ;
     8     
     9     public EmpDAOproxy () throws Exception{
    10         this.dbc = new DatabaseConnection() ;
    11         this.dao = new EmpDAOImpl(this.dbc.getConnection()) ;
    12     }
    13     
    14     public boolean doCreate(Emp emp) throws Exception {
    15         boolean flag = false ;
    16         try {
    17             if (this.dao.findById(emp.getEmpno()) == null){
    18                 this.dao.doCreate(emp) ;
    19                 flag = true ;
    20             }
    21         } catch (Exception e) {
    22             e.printStackTrace() ;
    23         } finally {
    24             this.dbc.close() ;
    25         }
    26         return flag ;
    27     }
    28 
    29     public List<Emp> findAll(String keyword) throws Exception {
    30         List<Emp> all = null ;
    31         try {
    32             all = this.dao.findAll(keyword) ;
    33         } catch (Exception e) {
    34             e.printStackTrace() ;
    35         } finally {
    36             this.dbc.close() ;
    37         }
    38         return all ;
    39     }
    40 
    41     public Emp findById(int empno) throws Exception {
    42         Emp emp = null ;
    43         try {
    44             this.dao.findById(empno) ;
    45         } catch (Exception e) {
    46             e.printStackTrace() ;
    47         } finally {
    48             this.dbc.close() ;
    49         }
    50         return emp ;
    51     }
    52 
    53 }

    4、定义工厂类 实例化 代理类 --> 调用实现接口类 完成操作

    1 package fafa.vo;
    2 
    3 public class DAOfactory {
    4     public static IempDAO getIEmpDAOInstance() throws Exception {
    5         return new EmpDAOproxy() ;
    6     }
    7 }

    5、当然少不了数据库连接类 DatabaseConnection 和vo 类 emp

     1 package fafa.vo;
     2 
     3 import java.sql.Connection;
     4 import java.sql.DriverManager;
     5 
     6 public class DatabaseConnection {
     7     private static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ;
     8     private static final String DBURL = "jdbc:mysql://localhost:3306/test" ;
     9     private static final String DBUSER = "root" ;
    10     private static final String DBPASS = "123456" ;
    11     
    12     private Connection conn ;
    13     public DatabaseConnection() throws Exception {
    14         Class.forName(DBDRIVER) ;
    15         this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS) ;
    16     }
    17     //取得连接
    18     public Connection getConnection(){
    19         return this.conn ;
    20     }
    21     //关闭连接
    22     public void close(){
    23         if (this.conn != null){
    24             try{
    25                 this.conn.close() ;
    26             }catch(Exception e){
    27                 e.printStackTrace() ;
    28             }
    29         }
    30     }
    31 }
     1 package fafa.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 hirdate ;
    10     private float sal ;
    11     
    12     public int getEmpno() {
    13         return empno;
    14     }
    15     public void setEmpno(int empno) {
    16         this.empno = empno;
    17     }
    18     public String getEname() {
    19         return ename;
    20     }
    21     public void setEname(String ename) {
    22         this.ename = ename;
    23     }
    24     public String getJob() {
    25         return job;
    26     }
    27     public void setJob(String job) {
    28         this.job = job;
    29     }
    30     public Date getHirdate() {
    31         return hirdate;
    32     }
    33     public void setHirdate(Date hirdate) {
    34         this.hirdate = hirdate;
    35     }
    36     public float getSal() {
    37         return sal;
    38     }
    39     public void setSal(float sal) {
    40         this.sal = sal;
    41     }
    42 }
  • 相关阅读:
    11111 Generalized Matrioshkas
    Uva 442 Matrix Chain Multiplication
    Uva 10815 Andy's First Dictionary
    Uva 537 Artificial Intelligence?
    Uva 340 MasterMind Hints
    SCAU 9508 诸葛给我牌(水泥题)
    Uva 10420 List of Conquests(排序水题)
    Uva 409 Excuses, Excuses!
    10/26
    11/2
  • 原文地址:https://www.cnblogs.com/dafa/p/2770944.html
Copyright © 2011-2022 走看看