大家说怎么开发公用的数据库操作包阿?能不能给个范例阿? 你直接用ejb不行吗?自己开发什么样的数据库操作包啊? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 阿? 难道大家都是直接写ejb?数据库操作包,实现执行sql 语句,调用存储过程等是公用组件大家都不开发??晕! 偶觉得开发这种包是费力不讨好的事情,是传入SQL语句还是组合SQL语句?基本上对数据库操作的要根据具体业务分别封装(除非是公共查询?),也许是我孤陋寡闻,从没听说过有数据库查询的公共组件! 楼上的几位,不敢苟同,我们就是这样写的1、数据库连接池2、执行SQL的类,返回一个对象(不是ResultSet)3、返回对象的class ,实现按字段取。按顺序取等当然可以构造更灵活更复杂的通用包 ConnectionPool.java:public interface ConnectionPool{Connection getConnection()throws test.res.ResourceNotAvailableException, SQLException;Connection getConnection(long timeout)throws test.res.ResourceTimeOutException, SQLException;void clear();}ConnectionHome.java:public interface ConnectionHome{void releaseConnection(Connection conn);}ConnectionPooling.java:public interface ConnectionPooling extends ConnectionHome{Connection getConnection()throws test.res.ResourceNotAvailableException, SQLException;Connection getConnection(long timeout)throws test.res.ResourceTimeOutException, SQLException;void clear();void releaseConnection(Connection conn);}ConnectionFactory.java:public interface ConnectionFactory{public Connection createConnection()throws SQLException;}PooledConnection.java: (for Connection in Java 1.1.8)public final class PooledConnection implements Connection{private interface ConnectionState{ConnectionState close();boolean isClosed();Connection getOpenConnection()throws SQLException;}private static class ClosedConnection implements ConnectionState{public final ConnectionState close(){return this;}public final Connection getOpenConnection()throws SQLException{throw new SQLException("Connection closed");}public final boolean isClosed(){return true;}private ClosedConnection(){}private static final ConnectionState _instance = new ClosedConnection();static ConnectionState instance(Connection conn, ConnectionHome home){return _instance;}}private static class OpenConnection implements ConnectionState{private final ConnectionHome home;private final Connection conn;public final ConnectionState close(){home.releaseConnection(conn);return ClosedConnection.instance(conn, home);}public final Connection getOpenConnection(){return conn;}public final boolean isClosed(){return false;}OpenConnection(Connection conn, ConnectionHome home){this.conn = conn; this.home = home;}static ConnectionState instance(Connection conn, ConnectionHome home){return new OpenConnection(conn, home);}}private ConnectionState state;public static Connection decorate(Connection conn, ConnectionHome home)throws SQLException{return new PooledConnection(conn, home);}private PooledConnection(Connection conn, ConnectionHome home)throws SQLException{if(conn.isClosed()){state = ClosedConnection.instance(conn, home);}else{state = OpenConnection.instance(conn, home);}} public final boolean isClosed(){return state.isClosed();}public final void close(){state = state.close();}protected void finalize(){close();}private final Connection getOpenConnection()throws SQLException{return state.getOpenConnection();}/*****then, delegate all the other methods****/public final Statement createStatement()throws SQLException{return getOpenConnection().createStatement();}//....public final void clearWarnings()throws SQLException{getOpenConnection().clearWarnings();}public final void commit()throws SQLException{getOpenConnection().commit();}/*public final Statement createStatement(int resultSetType, int resultSetConcurrency)throws SQLException{return getOpenConnection().createStatement(resultSetType, resultSetConcurrency);}*//*public final Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)throws SQLException{return getOpenConnection().createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);}*/public final boolean getAutoCommit()throws SQLException{return getOpenConnection().getAutoCommit();}public final String getCatalog()throws SQLException{return getOpenConnection().getCatalog();}/*public final int getHoldability()throws SQLException{return getOpenConnection().getHoldability();}*/public final DatabaseMetaData getMetaData()throws SQLException{return getOpenConnection().getMetaData();}public final int getTransactionIsolation()throws SQLException{return getOpenConnection().getTransactionIsolation();}/*public final Map getTypeMap()throws SQLException{return getOpenConnection().getTypeMap();}*/public final SQLWarning getWarnings()throws SQLException{return getOpenConnection().getWarnings();} public final boolean isReadOnly()throws SQLException{return getOpenConnection().isReadOnly();}public final String nativeSQL(String sql)throws SQLException{return getOpenConnection().nativeSQL(sql);}public final CallableStatement prepareCall(String sql)throws SQLException{return getOpenConnection().prepareCall(sql);}/*public final CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency)throws SQLException{return getOpenConnection().prepareCall(sql, resultSetType, resultSetConcurrency);}public final CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)throws SQLException{return getOpenConnection().prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);}*/public final PreparedStatement prepareStatement(String sql)throws SQLException{return getOpenConnection().prepareStatement(sql);}/*public final PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)throws SQLException{return getOpenConnection().prepareStatement(sql, autoGeneratedKeys);}public final PreparedStatement prepareStatement(String sql, int[] columnIndexes)throws SQLException{return getOpenConnection().prepareStatement(sql, columnIndexes);}public final PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)throws SQLException{return getOpenConnection().prepareStatement(sql, resultSetType, resultSetConcurrency);} 继续public final PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)throws SQLException{return getOpenConnection().prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);}public final PreparedStatement prepareStatement(String sql, String[] columnNames)throws SQLException{return getOpenConnection().prepareStatement(sql, columnNames);}public final void releaseSavepoint(Savepoint savepoint)throws SQLException{getOpenConnection().releaseSavepoint(savepoint);}*/public final void rollback()throws SQLException{getOpenConnection().rollback();}/*public final void rollback(Savepoint savepoint)throws SQLException{getOpenConnection().rollback(savepoint);}*/public final void setAutoCommit(boolean autoCommit)throws SQLException{getOpenConnection().setAutoCommit(autoCommit);}public final void setCatalog(String catalog)throws SQLException{getOpenConnection().setCatalog(catalog);}/* public final void setHoldability(int holdability)throws SQLException{getOpenConnection().setHoldability(holdability);}*/ public final void setReadOnly(boolean readOnly)throws SQLException{getOpenConnection().setReadOnly(readOnly);}/*public final Savepoint setSavepoint()throws SQLException{return getOpenConnection().setSavepoint();}public final Savepoint setSavepoint(String name)throws SQLException{return getOpenConnection().setSavepoint(name);}*/public final void setTransactionIsolation(int level)throws SQLException{getOpenConnection().setTransactionIsolation(level);}/*public final void setTypeMap(Map map)throws SQLException{getOpenConnection().setTypeMap(map);}*//*************************************************************************************************/}ConnectionPooling2Pool.java:public class ConnectionPooling2Pool implements ConnectionPool{public final Connection getConnection()throws test.res.ResourceNotAvailableException, SQLException{return PooledConnection.decorate(pooling.getConnection(), pooling);}public final Connection getConnection(long timeout)throws test.res.ResourceTimeOutException, SQLException{return PooledConnection.decorate(pooling.getConnection(timeout), pooling);}public final void clear(){pooling.clear();}private final ConnectionPooling pooling;private ConnectionPooling2Pool(ConnectionPooling pooling){this.pooling = pooling;}public static ConnectionPool decorate(ConnectionPooling pooling){return new ConnectionPooling2Pool(pooling);}}ConnectionPoolingImpl.java: (a simple implementation of ConnectionMan)public class ConnectionPoolingImpl implements ConnectionPooling{private int client=0;private final Vector freeConn = new Vector();private final int maxConn;private final ConnectionFactory factory;static public ConnectionPooling instance(ConnectionFactory factory, int max){return new ConnectionPoolingImpl(factory, max);}private ConnectionPoolingImpl(ConnectionFactory factory, int max){this.factory = factory;this.maxConn = max;}public final synchronized void releaseConnection(Connection conn){freeConn.addElement(conn);client--;notify();}public final synchronized Connection getConnection()throws ResourceNotAvailableException, SQLException{Connection conn = null;if(freeConn.size() > 0){conn = (Connection)freeConn.lastElement();freeConn.removeElementAt(freeConn.size()-1);}else if(client < maxConn){conn = factory.createConnection();}if(conn != null){client++;return conn;}else{throw new ResourceNotAvailableException();}}public final synchronized Connection getConnection(long timeout)throws ResourceTimeOutException, SQLException{for(long startTime = new java.util.Date().getTime();;){try{return getConnection();}catch(ResourceNotAvailableException e1){try{wait(timeout);}catch(InterruptedException e2){}if((new java.util.Date().getTime() - startTime) >= timeout){throw new ResourceTimeOutException();}}}}public final synchronized int getfreeconn(){return freeConn.size();}public final int getmaxConn(){return maxConn;}public final synchronized int getclient(){return client;}public final synchronized void setclient(){client=0;}public final synchronized void clear(){closeAll();freeConn.removeAllElements();}private final void closeAll(){for(int i=0; i<freeConn.size();i++){final Connection conn = (Connection)freeConn.elementAt(i);try{conn.close();}catch(SQLException sqlException){}}}protected void finalize(){closeAll();} }ConnectionFactoryImpl.java:public class ConnectionFactoryImpl{private ConnectionFactoryImpl(){}static public ConnectionFactory instance(final String driver, final String url, final String user, final String pwd)throws SQLException, ClassNotFoundException{final Class driverClass = Class.forName(driver);return new ConnectionFactory(){private final Class keeper = driverClass;public final Connection createConnection()throws SQLException{return DriverManager.getConnection(url,user,pwd);}};}static public ConnectionFactory instance(final String driver, final String url)throws SQLException, ClassNotFoundException{final Class driverClass = Class.forName(driver);return new ConnectionFactory(){private final Class keeper = driverClass;public final Connection createConnection()throws SQLException{return DriverManager.getConnection(url);}};} }TestConnectionPool.java:public class TestConnectionPool{public static void test(String driver, String url, String user, String pwd)throws java.sql.SQLException, test.res.ResourceNotAvailableException, test.res.ResourceTimeOutException, ClassNotFoundException{final ConnectionPool pool = ConnectionPooling2Pool.decorate(ConnectionPoolingImpl.instance(ConnectionFactoryImpl.instance(driver, url, user, pwd),1000));}}ResourceNotAvailableException.java:public class ResourceNotAvailableException extends RuntimeException{public ResourceNotAvailableException(String msg){super(msg);}public ResourceNotAvailableException(){}}ResourceTimeOutException.java:public class ResourceTimeOutException extends Exception{public ResourceTimeOutException(String msg){super(msg);}public ResourceTimeOutException(){}} 从以上面的几位朋友的言论可以看出,很多人还处在初级阶段,满足于用insert, delete ,update 来完成工作,做小打小闹的玩艺是可以,做一个像样的工程就不行,要不就是 sql 满天飞。我想楼主说的应该是类似于 PowerBuilder中的 DataStore 对象,屏蔽了基础SQL语句,提供了对数据库的缓冲访问,将对数据库的访问抽象出来,用同一的方式来访问数据库而不用管数据库的类型,并解决不同数据库不同SQL语句写法带来的麻烦。 用EJB简单的不得了阿何必自寻烦恼,想抽象操作用Entity EJB,想连接不同的数据库用datasource,多好 从以上面的几位朋友的言论可以看出,很多人还处在初级阶段,满足于用insert, delete ,update 来完成工作,做小打小闹的玩艺是可以,做一个像样的工程就不行,要不就是 sql 满天飞。我想楼主说的应该是类似于 PowerBuilder中的 DataStore 对象,屏蔽了基础SQL语句,提供了对数据库的缓冲访问,将对数据库的访问抽象出来,用同一的方式来访问数据库而不用管数据库的类型,并解决不同数据库不同SQL语句写法带来的麻烦。---------------------------------TO: z_j() ( )要做类似与PB的DATASTORE对象只怕不只是一个控件问题,而是要有一个匹配的开发环境,象PB、VB、DELPHI、.NET这些封装了数据库操作控件的开发软件,其关键不是该控件本身,而是其可视化的开发环境。“解决不同数据库不同SQL语句写法带来的麻烦。”如果是用标准的SQL语句,这个问题只怕不用解决,如果是各数据库的扩展SQL……,其实不难,不过很烦! 封装统一的数据访问组件是肯定的,这方面有很多值得借鉴的例子,可以参考的地方也很多,主要都是一些著名的设计模式的站点,如:SUN的开发者论坛、THE SERVER SIDE等,都介绍了不少解决持久性框架的问题。还有不少书籍也是介绍这方面的,我现在正在看WROX的"J2EE Design Patterns Applied",适用于我这样的初学者,建议没有涉足这方面的朋友看看。因为大家用JAVA,不是在用VB,我们要用面向对象的观点看待程序设计,不能只停留在画表单,写IF判断语句的基础上。 楼主的想法是很好的。应该有类似的产品了,大家不妨去http://www.objectarchitects.de/找找。此外,MS在"ADO.Net"的基础上封装了一个"SQLHelper",应该和楼主所说的功能差不多,到MSDN看看相关资料希望会有启发。 看了一下刚才贴出的源码,看起来结构挺不错,还有state,proxy,factory,能用的都用上了,但是pool的核心写的不怎么样,原因有1。 采用vector数据结构本身就效率不高了,况且还元素数量没有初始化。2。 不会自适应,没人访问了,也拉着一大把连接。3。 频繁的操作vector4。 没有对连接池的一些数据统计一个pool也就介于Object和Micro-Architecture之间,这样就已经过度设计了。同时,用EJB不用ADO的人都是只知道EJB而不管为什么要用EJB,没有ADO的项目都是小项目。现在有很多ADO的工具,caster, 还有JDO,OJB什么的,自己写也完全可以。 一个struts的设计如何整合hibernate和spring? java中有没有其它更好用的持久层框架? 谁能介绍本学习Struts2的好书? 使用struts2出现的一个怪问题.请帮我看看 关于图片上传的问题 我在使用struts框架,谁有Validator页面验证的服务器和客户端代码?以及这方面的电子书和教程,谢谢啊 关于native方法的问题 一个JB+WEBLGOIC+MSSQL配置的错误!!急!! Ejb中怎么访问远程接口函数? 为什么金山词霸不能翻译JBUILDER7里的HELP文件?? 问一个JDBC的问题 急 急 急 在有状态bean中保存不了状态,出错,帮忙看看
数据库操作包,实现执行sql 语句,调用存储过程等
是公用组件
大家都不开发??
晕!
1、数据库连接池
2、执行SQL的类,返回一个对象(不是ResultSet)
3、返回对象的class ,实现按字段取。按顺序取等
当然可以构造更灵活更复杂的通用包
public interface ConnectionPool{
Connection getConnection()
throws test.res.ResourceNotAvailableException, SQLException;
Connection getConnection(long timeout)
throws test.res.ResourceTimeOutException, SQLException;
void clear();
}
ConnectionHome.java:
public interface ConnectionHome{
void releaseConnection(Connection conn);
}
ConnectionPooling.java:
public interface ConnectionPooling extends ConnectionHome{
Connection getConnection()
throws test.res.ResourceNotAvailableException, SQLException;Connection getConnection(long timeout)
throws test.res.ResourceTimeOutException, SQLException;
void clear();
void releaseConnection(Connection conn);
}
ConnectionFactory.java:
public interface ConnectionFactory{
public Connection createConnection()throws SQLException;
}
PooledConnection.java: (for Connection in Java 1.1.8)public final class PooledConnection implements Connection{
private interface ConnectionState{
ConnectionState close();
boolean isClosed();
Connection getOpenConnection()
throws SQLException;
}
private static class ClosedConnection implements ConnectionState{
public final ConnectionState close(){return this;}
public final Connection getOpenConnection()
throws SQLException{
throw new SQLException("Connection closed");
}
public final boolean isClosed(){return true;}
private ClosedConnection(){}
private static final ConnectionState _instance = new ClosedConnection();
static ConnectionState instance(Connection conn, ConnectionHome home){return _instance;}
}private static class OpenConnection implements ConnectionState{
private final ConnectionHome home;
private final Connection conn;
public final ConnectionState close(){
home.releaseConnection(conn);
return ClosedConnection.instance(conn, home);
}
public final Connection getOpenConnection()
{return conn;}
public final boolean isClosed(){return false;}
OpenConnection(Connection conn, ConnectionHome home){
this.conn = conn; this.home = home;
}
static ConnectionState instance(Connection conn, ConnectionHome home){
return new OpenConnection(conn, home);
}
}
private ConnectionState state;
public static Connection decorate(Connection conn, ConnectionHome home)
throws SQLException{
return new PooledConnection(conn, home);
}private PooledConnection(Connection conn, ConnectionHome home)
throws SQLException{
if(conn.isClosed()){
state = ClosedConnection.instance(conn, home);
}
else{
state = OpenConnection.instance(conn, home);
}
}
public final boolean isClosed(){
return state.isClosed();
}
public final void close(){
state = state.close();
}
protected void finalize(){
close();
}
private final Connection getOpenConnection()
throws SQLException
{return state.getOpenConnection();}
/*****then, delegate all the other methods****/
public final Statement createStatement()
throws SQLException{
return getOpenConnection().createStatement();
}//....
public final void clearWarnings()throws SQLException{
getOpenConnection().clearWarnings();
}
public final void commit()throws SQLException{
getOpenConnection().commit();
}
/*
public final Statement createStatement(int resultSetType,
int resultSetConcurrency)
throws SQLException{
return getOpenConnection().createStatement(resultSetType, resultSetConcurrency);
}*//*
public final Statement createStatement(int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException{
return getOpenConnection().createStatement(resultSetType,
resultSetConcurrency, resultSetHoldability);
}*/
public final boolean getAutoCommit()throws SQLException{
return getOpenConnection().getAutoCommit();
}
public final String getCatalog()throws SQLException{
return getOpenConnection().getCatalog();
}
/*
public final int getHoldability()throws SQLException{
return getOpenConnection().getHoldability();
}*/public final DatabaseMetaData getMetaData()throws SQLException{
return getOpenConnection().getMetaData();
}
public final int getTransactionIsolation()throws SQLException{
return getOpenConnection().getTransactionIsolation();
}
/*
public final Map getTypeMap()throws SQLException{
return getOpenConnection().getTypeMap();
}*/
public final SQLWarning getWarnings()throws SQLException{
return getOpenConnection().getWarnings();
}
public final boolean isReadOnly()throws SQLException{
return getOpenConnection().isReadOnly();
}
public final String nativeSQL(String sql)throws SQLException{
return getOpenConnection().nativeSQL(sql);
}
public final CallableStatement prepareCall(String sql)throws SQLException{
return getOpenConnection().prepareCall(sql);
}
/*
public final CallableStatement prepareCall(String sql,
int resultSetType, int resultSetConcurrency)
throws SQLException{
return getOpenConnection().prepareCall(sql, resultSetType, resultSetConcurrency);
}public final CallableStatement prepareCall(String sql,
int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException{
return getOpenConnection().prepareCall(sql, resultSetType,
resultSetConcurrency, resultSetHoldability);
}*/public final PreparedStatement prepareStatement(String sql)
throws SQLException{
return getOpenConnection().prepareStatement(sql);
}
/*
public final PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
throws SQLException{
return getOpenConnection().prepareStatement(sql, autoGeneratedKeys);
}
public final PreparedStatement prepareStatement(String sql, int[] columnIndexes)
throws SQLException{
return getOpenConnection().prepareStatement(sql, columnIndexes);
}public final PreparedStatement prepareStatement(String sql,
int resultSetType, int resultSetConcurrency)
throws SQLException{
return getOpenConnection().prepareStatement(sql,
resultSetType, resultSetConcurrency);
}
int resultSetType, int resultSetConcurrency, int resultSetHoldability)
throws SQLException{
return getOpenConnection().prepareStatement(sql,
resultSetType, resultSetConcurrency, resultSetHoldability);
}public final PreparedStatement prepareStatement(String sql,
String[] columnNames)
throws SQLException{
return getOpenConnection().prepareStatement(sql, columnNames);
}public final void releaseSavepoint(Savepoint savepoint)throws SQLException{
getOpenConnection().releaseSavepoint(savepoint);
}*/
public final void rollback()throws SQLException{
getOpenConnection().rollback();
}
/*
public final void rollback(Savepoint savepoint)
throws SQLException{
getOpenConnection().rollback(savepoint);
}*/public final void setAutoCommit(boolean autoCommit)
throws SQLException{
getOpenConnection().setAutoCommit(autoCommit);
}public final void setCatalog(String catalog)
throws SQLException{
getOpenConnection().setCatalog(catalog);
}
/*
public final void setHoldability(int holdability)
throws SQLException{
getOpenConnection().setHoldability(holdability);
}*/
public final void setReadOnly(boolean readOnly)
throws SQLException{
getOpenConnection().setReadOnly(readOnly);
}
/*
public final Savepoint setSavepoint()throws SQLException{
return getOpenConnection().setSavepoint();
}public final Savepoint setSavepoint(String name)
throws SQLException{
return getOpenConnection().setSavepoint(name);
}*/public final void setTransactionIsolation(int level)
throws SQLException{
getOpenConnection().setTransactionIsolation(level);
}
/*public final void setTypeMap(Map map)throws SQLException{
getOpenConnection().setTypeMap(map);
}*/
/*************************************************************************************************/}
ConnectionPooling2Pool.java:
public class ConnectionPooling2Pool implements ConnectionPool{
public final Connection getConnection()
throws test.res.ResourceNotAvailableException, SQLException{
return PooledConnection.decorate(pooling.getConnection(), pooling);
}
public final Connection getConnection(long timeout)
throws test.res.ResourceTimeOutException, SQLException{
return PooledConnection.decorate(pooling.getConnection(timeout), pooling);
}
public final void clear(){
pooling.clear();
}
private final ConnectionPooling pooling;
private ConnectionPooling2Pool(ConnectionPooling pooling){
this.pooling = pooling;
}
public static ConnectionPool decorate(ConnectionPooling pooling){
return new ConnectionPooling2Pool(pooling);
}
}
ConnectionPoolingImpl.java: (a simple implementation of ConnectionMan)
public class ConnectionPoolingImpl implements ConnectionPooling
{
private int client=0;
private final Vector freeConn = new Vector();
private final int maxConn;
private final ConnectionFactory factory;
static public ConnectionPooling instance(ConnectionFactory factory, int max){
return new ConnectionPoolingImpl(factory, max);
}private ConnectionPoolingImpl(ConnectionFactory factory, int max){
this.factory = factory;
this.maxConn = max;
}
public final synchronized void releaseConnection(Connection conn)
{
freeConn.addElement(conn);
client--;
notify();
}
public final synchronized Connection getConnection()
throws ResourceNotAvailableException, SQLException
{
Connection conn = null;
if(freeConn.size() > 0)
{
conn = (Connection)freeConn.lastElement();
freeConn.removeElementAt(freeConn.size()-1);
}
else if(client < maxConn)
{
conn = factory.createConnection();
}
if(conn != null)
{
client++;
return conn;
}
else
{
throw new ResourceNotAvailableException();
}
}
public final synchronized Connection getConnection(long timeout)
throws ResourceTimeOutException, SQLException
{
for(long startTime = new java.util.Date().getTime();;)
{
try
{
return getConnection();
}
catch(ResourceNotAvailableException e1)
{
try
{
wait(timeout);
}
catch(InterruptedException e2)
{}
if((new java.util.Date().getTime() - startTime) >= timeout)
{
throw new ResourceTimeOutException();
}
}
}
}public final synchronized int getfreeconn(){
return freeConn.size();
}
public final int getmaxConn(){
return maxConn;
}
public final synchronized int getclient(){
return client;
}
public final synchronized void setclient(){
client=0;
}
public final synchronized void clear(){
closeAll();
freeConn.removeAllElements();
}
private final void closeAll(){
for(int i=0; i<freeConn.size();i++)
{
final Connection conn = (Connection)freeConn.elementAt(i);
try{
conn.close();
}
catch(SQLException sqlException){}
}
}
protected void finalize(){
closeAll();
}
}ConnectionFactoryImpl.java:
public class ConnectionFactoryImpl
{
private ConnectionFactoryImpl(){}
static public ConnectionFactory instance(final String driver, final String url,
final String user, final String pwd)
throws SQLException, ClassNotFoundException{
final Class driverClass = Class.forName(driver);
return new ConnectionFactory(){
private final Class keeper = driverClass;
public final Connection createConnection()
throws SQLException{
return DriverManager.getConnection(url,user,pwd);
}
};
}
static public ConnectionFactory instance(final String driver, final String url)
throws SQLException, ClassNotFoundException{
final Class driverClass = Class.forName(driver);
return new ConnectionFactory(){
private final Class keeper = driverClass;
public final Connection createConnection()
throws SQLException{
return DriverManager.getConnection(url);
}
};
}
}
TestConnectionPool.java:
public class TestConnectionPool{
public static void test(String driver, String url, String user, String pwd)
throws java.sql.SQLException, test.res.ResourceNotAvailableException, test.res.ResourceTimeOutException, ClassNotFoundException{
final ConnectionPool pool = ConnectionPooling2Pool.decorate(
ConnectionPoolingImpl.instance(
ConnectionFactoryImpl.instance(driver, url, user, pwd),
1000)
);
}
}
ResourceNotAvailableException.java:
public class ResourceNotAvailableException extends RuntimeException{
public ResourceNotAvailableException(String msg){super(msg);}
public ResourceNotAvailableException(){}
}
ResourceTimeOutException.java:
public class ResourceTimeOutException extends Exception{
public ResourceTimeOutException(String msg){super(msg);}
public ResourceTimeOutException(){}
}
---------------------------------
TO: z_j() ( )要做类似与PB的DATASTORE对象只怕不只是一个控件问题,而是要有一个匹配的开发环境,象PB、VB、DELPHI、.NET这些封装了数据库操作控件的开发软件,其关键不是该控件本身,而是其可视化的开发环境。
“解决不同数据库不同SQL语句写法带来的麻烦。”如果是用标准的SQL语句,这个问题只怕不用解决,如果是各数据库的扩展SQL……,其实不难,不过很烦!
此外,MS在"ADO.Net"的基础上封装了一个"SQLHelper",应该和楼主所说的功能差不多,到MSDN看看相关资料希望会有启发。
1。 采用vector数据结构本身就效率不高了,况且还元素数量没有初始化。
2。 不会自适应,没人访问了,也拉着一大把连接。
3。 频繁的操作vector
4。 没有对连接池的一些数据统计
一个pool也就介于Object和Micro-Architecture之间,这样就已经过度设计了。同时,用EJB不用ADO的人都是只知道EJB而不管为什么要用EJB,没有ADO的项目都是小项目。现在有很多ADO的工具,caster, 还有JDO,OJB什么的,自己写也完全可以。