⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 daofactory.java

📁 又一个课程设计 简易 JSP 论坛 功能较简单的那种, 界面上模仿了 Discuz JSP 本来就学的不行, 只是尽量实现了 MVC
💻 JAVA
字号:
package cn.ialvin.bbs.dao;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;

import cn.ialvin.sql.ConnectionPool;
import cn.ialvin.sql.DBConnection;

public class DAOFactory {
	public boolean testDBType() {
		String cls = "cn.ialvin.bbs.dao." + DAOFactory.DBType + "UserDAO";
		try {
			Class.forName(cls);
			return true;
		} catch (ClassNotFoundException e) {
			return false;
		}
	}
	public DAOFactory(ConnectionPool pool) throws ClassNotFoundException {
		this.pool = pool;
		if (DAOFactory.DBType == null) {
			DAOFactory.DBType = pool.getConfig().getDBType();
		}
	}

	public IManagerDAO getManagerDAO() throws SQLException {
		return (IManagerDAO)this.getDAO("Manager");
	}

	public IForumDAO getForumDAO() throws SQLException {
		return (IForumDAO)this.getDAO("Forum");
	}

	public ICommentDAO getCommentDAO() throws SQLException {
		return (ICommentDAO)this.getDAO("Comment");
	}

	public IUserDAO getUserDAO() throws SQLException {
		return (IUserDAO)this.getDAO("User");
	}

	public ITopicDAO getTopicDAO() throws SQLException {
		return (ITopicDAO)this.getDAO("Topic");
	}

	private Object getDAO(String daoType)
			throws SQLException {
		if (this.coxn == null)
			this.coxn = new DBConnection(this.pool);
		String clsn = "cn.ialvin.bbs.dao." + DAOFactory.DBType + daoType + "DAO";
		try {
			Class cls = Class.forName(clsn);
			Constructor constructor = cls.getDeclaredConstructor(new Class[]{DBConnection.class});
			Object dao = constructor.newInstance(new Object[]{this.coxn});
			return dao;
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return null;
	}

	private ConnectionPool pool = null;
	private DBConnection coxn = null;
	private static String DBType = null;
	public void destroy() { if (this.coxn != null) this.coxn.close(); this.coxn = null; }
	public void finalize() throws Throwable { this.destroy(); super.finalize(); }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -