现在感觉之前学习的java就是个渣渣。完全没掌握好。
DAO设计模式 1):数据库结构 CREATE TABLE emp( empno INT(4) PRIMARY KEY, ename VARCHAR(10), job VARCHAR(9), hiredate DATE, sal FLOAT(7,2) ) ; 2):Emp.java package cn.mldn.lxh.vo ; import java.util.Date ; public class Emp { private int empno ; private String ename ; private String job ; private Date hiredate ; private float sal ; public void setEmpno(int empno){ this.empno = empno ; } public void setEname(String ename){ this.ename = ename ; } public void setJob(String job){ this.job = job ; } public void setHiredate(Date hiredate){ this.hiredate = hiredate ; } public void setSal(float sal){ this.sal = sal ; } public int getEmpno(){ return this.empno ; } public String getEname(){ return this.ename ; } public String getJob(){ return this.job ; } public Date getHiredate(){ return this.hiredate ; } public float getSal(){ return this.sal ; } } 3):IEmpDAO.java 功能接口 package cn.mldn.lxh.dao ; import java.util.* ; import cn.mldn.lxh.vo.* ; public interface IEmpDAO { public boolean doCreate(Emp emp) throws Exception ; public List<Emp> findAll(String keyWord) throws Exception ; public Emp findById(int empno) throws Exception ; } 4):EmpDAOImpl.java 接口实现 package cn.mldn.lxh.dao.impl ; import java.util.* ; import java.sql.* ; import cn.mldn.lxh.dao.* ; import cn.mldn.lxh.vo.* ; public class EmpDAOImpl implements IEmpDAO { private Connection conn = null ; private PreparedStatement pstmt = null ; public EmpDAOImpl(Connection conn){ this.conn = conn ; } public boolean doCreate(Emp emp) throws Exception{ boolean flag = false ; String sql = "INSERT INTO emp(empno,ename,job,hiredate,sal) VALUES (?,?,?,?,?)" ; this.pstmt = this.conn.prepareStatement(sql) ; this.pstmt.setInt(1,emp.getEmpno()) ; this.pstmt.setString(2,emp.getEname()) ; this.pstmt.setString(3,emp.getJob()) ; this.pstmt.setDate(4,new java.sql.Date(emp.getHiredate().getTime())) ; this.pstmt.setFloat(5,emp.getSal()) ; if(this.pstmt.executeUpdate() > 0){ flag = true ; } this.pstmt.close() ; return flag ; } public List<Emp> findAll(String keyWord) throws Exception{ List<Emp> all = new ArrayList<Emp>() ; String sql = "SELECT empno,ename,job,hiredate,sal FROM emp WHERE ename LIKE ? OR job LIKE ?" ; this.pstmt = this.conn.prepareStatement(sql) ; this.pstmt.setString(1,"%"+keyWord+"%") ; this.pstmt.setString(2,"%"+keyWord+"%") ; ResultSet rs = this.pstmt.executeQuery() ; Emp emp = null ; while(rs.next()){ emp = new Emp() ; emp.setEmpno(rs.getInt(1)) ; emp.setEname(rs.getString(2)) ; emp.setJob(rs.getString(3)) ; emp.setHiredate(rs.getDate(4)) ; emp.setSal(rs.getFloat(5)) ; all.add(emp) ; } this.pstmt.close() ; return all ; } public Emp findById(int empno) throws Exception{ Emp emp = null ; String sql = "SELECT empno,ename,job,hiredate,sal FROM emp WHERE empno=?" ; this.pstmt = this.conn.prepareStatement(sql) ; this.pstmt.setInt(1,empno) ; ResultSet rs = this.pstmt.executeQuery() ; if(rs.next()){ emp = new Emp() ; emp.setEmpno(rs.getInt(1)) ; emp.setEname(rs.getString(2)) ; emp.setJob(rs.getString(3)) ; emp.setHiredate(rs.getDate(4)) ; emp.setSal(rs.getFloat(5)) ; } this.pstmt.close() ; return emp ; } } 5):DatabaseConnection.java 数据库的对象的创建 package cn.mldn.lxh.dbc ; import java.sql.Connection ; import java.sql.DriverManager ; public class DatabaseConnection { private static final String DBDRIVER = "org.gjt.mm.mysql.Driver" ; private static final String DBURL = "jdbc:mysql://localhost:3306/mldn" ; private static final String DBUSER = "root" ; private static final String DBPASSWORD = "mysqladmin" ; private Connection conn ; public DatabaseConnection() throws Exception { Class.forName(DBDRIVER) ; this.conn = DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD) ; } public Connection getConnection(){ return this.conn ; } public void close() throws Exception { if(this.conn != null){ try{ this.conn.close() ; }catch(Exception e){ throw e ; } } } } 6):EmpDAOProxy.java 操作代理 package cn.mldn.lxh.dao.proxy ; import java.util.* ; import java.sql.* ; import cn.mldn.lxh.dao.* ; import cn.mldn.lxh.dbc.* ; import cn.mldn.lxh.dao.impl.* ; import cn.mldn.lxh.vo.* ; public class EmpDAOProxy implements IEmpDAO { private DatabaseConnection dbc = null ; private IEmpDAO dao = null ; public EmpDAOProxy() throws Exception { this.dbc = new DatabaseConnection() ; this.dao = new EmpDAOImpl(this.dbc.getConnection()) ; } public boolean doCreate(Emp emp) throws Exception{ boolean flag = false ; try{ if(this.dao.findById(emp.getEmpno()) == null){ flag = this.dao.doCreate(emp) ; } }catch(Exception e){ throw e ; }finally{ this.dbc.close() ; } return flag ; } public List<Emp> findAll(String keyWord) throws Exception{ List<Emp> all = null ; try{ all = this.dao.findAll(keyWord) ; }catch(Exception e){ throw e ; }finally{ this.dbc.close() ; } return all ; } public Emp findById(int empno) throws Exception{ Emp emp = null ; try{ emp = this.dao.findById(empno) ; }catch(Exception e){ throw e ; }finally{ this.dbc.close() ; } return emp ; } } 7):DAOFactory.java 工厂 实例化操作代理对象 package cn.mldn.lxh.factory ; import cn.mldn.lxh.dao.IEmpDAO ; import cn.mldn.lxh.dao.proxy.EmpDAOProxy ; public class DAOFactory { public static IEmpDAO getIEmpDAOInstance() throws Exception{ return new EmpDAOProxy() ; } }