| class theOnClose implements OnConnectionClose { private Vector v; public theOnClose(Vector vt) { v = vt; } public void Action(PoolConnection sender) { v.remove(sender); } } class PoolConnection implements Connection , Serializable{ private Connection aCon = null; private boolean closed = false; private boolean inUse = false; private String DriverName; private OnConnectionClose onClose = null; private void writeObject(ObjectOutputStream oos) throws IOException { oos.defaultWriteObject(); } private void readObject(ObjectInputStream ois) throws ClassNotFoundException, IOException { ois.defaultReadObject(); } protected PoolConnection() { } public PoolConnection(String Url, String User, String Password) throws SQLException { aCon = DriverManager.getConnection(Url, User, Password); closed = false; inUse=true; } public PoolConnection(String Url) throws Exception { aCon = DriverManager.getConnection(Url); closed = false; inUse=true; } public Statement createStatement() throws SQLException { /**@todo Implement this java.sql.Connection method*/ //throw new java.lang.UnsupportedOperationException("Method createStatement() not yet implemented."); return aCon.createStatement(); } public PreparedStatement prepareStatement(String sql) throws SQLException { /**@todo Implement this java.sql.Connection method*/ //throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented."); return aCon.prepareStatement(sql); } public CallableStatement prepareCall(String sql) throws SQLException { /**@todo Implement this java.sql.Connection method*/ //throw new java.lang.UnsupportedOperationException("Method prepareCall() not yet implemented."); return aCon.prepareCall(sql); } public String nativeSQL(String sql) throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method nativeSQL() not yet implemented."); return aCon.nativeSQL(sql); } public void setAutoCommit(boolean autoCommit) throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method setAutoCommit() not yet implemented."); aCon.setAutoCommit(autoCommit); } public boolean getAutoCommit() throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method getAutoCommit() not yet implemented."); return aCon.getAutoCommit(); } public void commit() throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method commit() not yet implemented."); aCon.commit(); } public void rollback() throws SQLException { /**@todo Implement this java.sql.Connection method*/ //throw new java.lang.UnsupportedOperationException("Method rollback() not yet implemented."); aCon.rollback(); } public void close() throws SQLException { /**@todo Implement this java.sql.Connection method*/ //throw new java.lang.UnsupportedOperationException("Method close() not yet implemented."); if(DBConnectionPool.getCout()<=DBConnectionPool.getMinCount()) { inUse = false; }else { closeConnection(); } } public boolean isClosed() throws SQLException { /**@todo Implement this java.sql.Connection method*/ //throw new java.lang.UnsupportedOperationException("Method isClosed() not yet implemented."); return closed; } public DatabaseMetaData getMetaData() throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method getMetaData() not yet implemented."); return aCon.getMetaData(); } public void setReadOnly(boolean readOnly) throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method setReadOnly() not yet implemented."); aCon.setReadOnly(readOnly); } public boolean isReadOnly() throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method isReadOnly() not yet implemented."); return isReadOnly(); } public void setCatalog(String catalog) throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method setCatalog() not yet implemented."); aCon.setCatalog(catalog); } public String getCatalog() throws SQLException { /**@todo Implement this java.sql.Connection method*/ //throw new java.lang.UnsupportedOperationException("Method getCatalog() not yet implemented."); return aCon.getCatalog(); } public void setTransactionIsolation(int level) throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method setTransactionIsolation() not yet implemented."); aCon.setTransactionIsolation(level); } public int getTransactionIsolation() throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method getTransactionIsolation() not yet implemented."); return aCon.getTransactionIsolation(); } public SQLWarning getWarnings() throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method getWarnings() not yet implemented."); return aCon.getWarnings(); } public void clearWarnings() throws SQLException { /**@todo Implement this java.sql.Connection method*/ //throw new java.lang.UnsupportedOperationException("Method clearWarnings() not yet implemented."); aCon.clearWarnings(); } public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method createStatement() not yet implemented."); return aCon.createStatement(resultSetType, resultSetConcurrency); } public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented."); return aCon.prepareStatement(sql, resultSetType, resultSetConcurrency); } public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException { /**@todo Implement this java.sql.Connection method*/ //throw new java.lang.UnsupportedOperationException("Method prepareCall() not yet implemented."); return aCon.prepareCall(sql, resultSetType, resultSetConcurrency); } public Map getTypeMap() throws SQLException { /**@todo Implement this java.sql.Connection method*/ //throw new java.lang.UnsupportedOperationException("Method getTypeMap() not yet implemented."); return aCon.getTypeMap(); } public void setTypeMap(Map map) throws SQLException { /**@todo Implement this java.sql.Connection method*/ //throw new java.lang.UnsupportedOperationException("Method setTypeMap() not yet implemented."); aCon.setTypeMap(map); } public void setHoldability(int holdability) throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method setHoldability() not yet implemented."); aCon.setHoldability(holdability); } public int getHoldability() throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method getHoldability() not yet implemented."); return aCon.getHoldability(); } public Savepoint setSavepoint() throws SQLException { /**@todo Implement this java.sql.Connection method*/ //throw new java.lang.UnsupportedOperationException("Method setSavepoint() not yet implemented."); return setSavepoint(); } public Savepoint setSavepoint(String name) throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method setSavepoint() not yet implemented."); return setSavepoint(name); } public void rollback(Savepoint savepoint) throws SQLException { /**@todo Implement this java.sql.Connection method*/ //throw new java.lang.UnsupportedOperationException("Method rollback() not yet implemented."); aCon.rollback(savepoint); } public void releaseSavepoint(Savepoint savepoint) throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method releaseSavepoint() not yet implemented."); aCon.releaseSavepoint(savepoint); } public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { /**@todo Implement this java.sql.Connection method*/ //throw new java.lang.UnsupportedOperationException("Method createStatement() not yet implemented."); return aCon.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability); } public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented."); return aCon.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability); } public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException { /**@todo Implement this java.sql.Connection method*/ //throw new java.lang.UnsupportedOperationException("Method prepareCall() not yet implemented."); return aCon.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability); } public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException { /**@todo Implement this java.sql.Connection method*/ //throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented."); return aCon.prepareStatement(sql, autoGeneratedKeys); } public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException { /**@todo Implement this java.sql.Connection method*/ // throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented."); return aCon.prepareStatement(sql, columnIndexes); } public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { /**@todo Implement this java.sql.Connection method*/ //throw new java.lang.UnsupportedOperationException("Method prepareStatement() not yet implemented."); return aCon.prepareStatement(sql, columnNames); } public void closeConnection() throws SQLException { if (onClose != null) { onClose.Action(this); } aCon.close(); } public boolean isUsed() { return inUse; } public void use() { inUse = true; } public void setOnClose(OnConnectionClose Action) { onClose = Action; } } |
以上就是我所写的连接池代码.
使用方法:
DBTools.DBConnectionPool.SetJDBC("jdbc:mysql://fireBird/trmg?useUnicode=true&characterEncoding=GB2312",
"Administrator","");
DBTools.DBConnectionPool.setDriverName("com.mysql.jdbc.Driver");
java.sql.Connection con = DBTools.DBConnectionPool.getConnection();
当使用完毕了别忘记将con关闭:).
好像现在使用java的人不允许人说java的问题,java的内存回收存在大问题.内存泄漏的厉害,建议如非必要不要使用new来生成新的对象.这样可能可以让我们的系统可以活的更长久一些.还有linux下java性能惨不忍睹,在俺测试的平台中win32反而是最高的.郁闷郁闷不是罪.
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
相关专题
- Java环境安装配置 (5658篇文章)
- Java编程开发手册 (8309篇文章)
- .NET移动与嵌入式技术 (5969篇文章)
- .NET开发手册 (5670篇文章)
- .NET应用研究 (495篇文章)
- .NET实用开发 (1686篇文章)
- 开发框架:深入了解 Struts Validator (3次浏览)
- Java中的通信机制及与C/C API的集成 (1次浏览)
- 用Hibernate实现领域对象的自定义字段 (1次浏览)
- 精通Hibernate之映射继承关系(一) (0次浏览)
- 精通Hibernate之映射继承关系(二) (0次浏览)
- 美国计算机教授语出惊人:Java对学生有害 (0次浏览)
- JDK 6 JRE 6 Update 4 (0次浏览)
- 三步教你改善Java代码质量 (0次浏览)
- Java语言入门 简述Java语言回收机制 (0次浏览)
- 2008年Java开发者最迫切的五个期望 (0次浏览)



