📄 daofactory.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 + -