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

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


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


DROP DATABASE IF EXISTS mldn;
CREATE DATABASE mldn;
USE mldn;
DROP TABLE IF EXISTS emp;
CREATE TABLE emp(
	empno	INT(4)	PRIMARY KEY,
	ename	VARCHAR(10),
	job		VARCHAR(9),
	hiredate	DATE,
	sal		FLOAT(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 block
				e.printStackTrace();
			}
		}
	}

}

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





  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值