DAO模式

<原文链接>

DAO(Data Access Object):数据访问对象     

属于JAVAEE的数据层的操作,用来完成数据库的访问,实现基本的CURD数据库操作

DAO包括五个部分:  

 1.   数据库连接类                  实现数据库连接封装的操作  

 2.    javabean类                    对应数据库中的表     每个bean对应一张表 

  3.   DAO接口                         定义了数据库的CURD等操作   用于给具体子类实现数据库操作

  4.  DAO实现类                       实现DAO接口 

  5 . DAO工厂类                       用来获取DAO实现类的实例   完成数据库操作    一般获取实例的方法为静态方法 

代码演示:  

  1.  数据库连接类

import Java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

//创建数据库操作类DatabaseConnection,主要负责数据库连接及关闭。
public class DbConnection {
	private final String driver = "com.mysql.jdbc.Driver";
	private final String url = "jdbc:mysql://localhost:3306/test";
	private final String dbuser = "littlebear";
	private final String dbpassword = "littlebear";
	private Connection conn = null;

	public DbConnection() {
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		try {
			this.conn = DriverManager.getConnection(url, dbuser, dbpassword);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	public Connection getConnection() {
		return this.conn;
	}

	public void closeConnection() {
		try {
			this.conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

2.  javabean  类

import com.lbwmx.dao.DaoFactory;

public class User {
	private int uid;
	private String name;
	private String password;

	public User() {
		super();
	}

	public User(int uid, String password) {
		this.uid = uid;
		this.password = password;
	}

	public int getUid() {
		return uid;
	}

	public void setUid(int uid) {
		this.uid = uid;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean LoginValidate() throws Exception {
		User user = (User) DaoFactory.getDaoInstance().findByUid(this.uid);
		if (user == null) {
			return false;
		}
		if (!user.getPassword().equals(this.password)) {
			return false; // error password
		}
		return true;
	}
}

3.  DAO接口类

package com.lbwmx.dao;

import java.util.List;
import com.lbwmx.bean.User;

public interface UserDao {
	public boolean doInsert(User user) throws Exception;

	public User findByUid(int uid) throws Exception;

	public List findAll() throws Exception;
}

4 .  DAO实现类   

package com.lbwmx.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.lbwmx.bean.User;

public class UserDaoImpl implements UserDao {
	private Connection conn = null;
	private PreparedStatement psmt = null;

	public UserDaoImpl(Connection conn) {
		this.conn = conn;
	}

	public boolean doInsert(User user) throws Exception {
		String insert = "insert into user(uid,name,password) values (?,?,?)";
		this.psmt = this.conn.prepareStatement(insert);
		this.psmt.setInt(1, user.getUid());
		this.psmt.setString(2, user.getName());
		this.psmt.setString(3, user.getPassword());
		int n = this.psmt.executeUpdate();
		this.psmt.close();
		if (n > 0) {
			return true;
		}
		return false;
	}

	public List findAll() throws Exception {
		String select = "select * from user";
		List list = new ArrayList();
		this.psmt = this.conn.prepareStatement(select);
		ResultSet rst = this.psmt.executeQuery();
		while (rst.next()) {
			User user = new User();
			user.setUid(rst.getInt("uid"));
			user.setName(rst.getString("name"));
			user.setPassword(rst.getString("password"));
			list.add(user);
		}
		this.psmt.close(); // 当关闭 psmt的时候,rst结果集也会随之关闭!
		// rst.close();
		return list;
	}

	public User findByUid(int uid) throws Exception {
		String sql = "select * from user where uid=?";
		this.psmt = this.conn.prepareStatement(sql);
		ResultSet rst = this.psmt.executeQuery();
		User user = null;
		if (rst.next()) {
			user = new User();
			user.setUid(rst.getInt("uid"));
			user.setName(rst.getString("name"));
			user.setPassword(rst.getString("password"));
		}
		this.psmt.close();
		return user;
	}
}

DAO代理类 

package com.lbwmx.dao;

import java.util.List;
import com.lbwmx.bean.User;
import com.lbwmx.mysql.DbConnection;

public class DaoProxy implements UserDao {
	private DbConnection dbConnection = null;
	private UserDao dao = null;

	public DaoProxy() {
		this.dbConnection = new DbConnection();
		this.dao = new UserDaoImpl(this.dbConnection.getConnection());
	}

	public boolean doInsert(User user) throws Exception {
		boolean flag = false;
		try {
			if (null == this.dao.findByUid(user.getUid())) {
				flag = this.dao.doInsert(user);
			}
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbConnection.closeConnection();
		}
		return flag;
	}

	public List findAll() throws Exception {
		List list = null;
		try {
			list = this.dao.findAll();
		} catch (Exception e) {
			throw e;
		} finally {
			this.dbConnection.closeConnection();
		}
		return list;
	}

	public User findByUid(int uid) throws Exception {
		User user = null;
		try {
			user = this.dao.findByUid(uid);
		} catch (Exception e) {
			throw e;
		}
		return user;
	}
}

5  .DAOFactory类   获取实现类实例

package com.lbwmx.dao;

public class DaoFactory {
	// 定义工厂类DaoFactory。工厂类通过DaoProxy实例化IDao对象,供调用者操作数据库。
	public static UserDao getDaoInstance() {
		return new DaoProxy();
	}
}

测试代码:   

User user = new User(1000,"password" );
         boolean flag =  DaoFactory.getDaoInstance().doInsert(user);
         if(flag){
   System.out.println("success");
}else{
         System.out.println("error");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值