Java中的DAO设计模式应用举例 ------> 来自Java WEB 开发实战经典

来源:互联网 发布:intellij java doc 编辑:程序博客网 时间:2024/06/03 19:13

项目的整体框架图如下表所示:


1、以mysql数据库作为本次演示的数据库,在数据库里面新建一个mldn,新建一个表,表名为emp


DROP DATABASE IF EXISTS mldn;CREATE DATABASE mldn;USE mldn;DROP TABLE IF EXISTS emp;CREATE TABLE emp(empnoINT(4)PRIMARY KEY,enameVARCHAR(10),jobVARCHAR(9),hiredateDATE,salFLOAT(7.2));

2、定义vo类

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 int getEmpno() {return empno;}public void setEmpno(int empno) {this.empno = empno;}public String getEname() {return ename;}public void setEname(String ename) {this.ename = ename;}public String getJob() {return job;}public void setJob(String job) {this.job = job;}public Date getHiredate() {return hiredate;}public void setHiredate(Date hiredate) {this.hiredate = hiredate;}public float getSal() {return sal;}public void setSal(float sal) {this.sal = sal;}}
3、定义数据库连接类

package cn.mldn.lxh.dbc;import java.sql.*;public class DatabaseConnection {private static final String DBDRIVER = "com.mysql.jdbc.Driver";private static final String DBURL = "jdbc:mysql://localhost:3306/mldn";private static final String DBUSER = "root";private static final String DBPASSWORD = "cisco";private Connection conn = null;public DatabaseConnection() throws Exception {try {Class.forName(DBDRIVER);this.conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD);} catch (Exception e) {throw e;}}public Connection getConnection() {return this.conn;}public void close() throws Exception {if (this.conn != null) {try {this.conn.close();} catch (Exception e) {throw e;}}}}

4、定义接口类

package cn.mldn.lxh.dao;import java.util.List;import cn.mldn.lxh.vo.Emp;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;}

5、定义接口的具体实现类

package cn.mldn.lxh.dao.impl;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.util.ArrayList;import java.util.List;import cn.mldn.lxh.dao.IEmpDAO;import cn.mldn.lxh.vo.Emp;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);//实例化PreparedStatement对象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 false;}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;}}

6、定义接口的实现代理类

package cn.mldn.lxh.dao.proxy;import java.util.List;import cn.mldn.lxh.dao.IEmpDAO;import cn.mldn.lxh.dao.impl.EmpDAOImpl;import cn.mldn.lxh.dbc.DatabaseConnection;import cn.mldn.lxh.vo.Emp;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、定义工厂类

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();}}

8、测试类

package cn.mldn.lxh.dao.test;import cn.mldn.lxh.factory.DAOFactory;import cn.mldn.lxh.vo.Emp;public class TestDAOInsert {public static void main(String[] args) {Emp emp =null;for(int x = 0;x<5;x++){emp = new Emp();emp.setEmpno(100+x);emp.setEname("李海超 -"+x);emp.setJob("程序员 -"+x);emp.setHiredate(new java.util.Date());emp.setSal(500*x);try {DAOFactory.getIEmpDAOInstance().doCreate(emp);System.out.println("Success");} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}}}}

9、测试结果截图如下,为mysql数据表的记录