一个通用的连接池Bean
时间:2008-05-07
//连接Bean ConnectionBean.javapackage com.pool;import java.io.Serializable;import java.sql.*;public class ConnectionBean implements java.io.Serializable{private Connection vConnection=null;private boolean bUseState=false;public ConnectionBean(){}public ConnectionBean(Connection connection){ if(connection!=null) { vConnection=connection; }}public Connection getConnection(){ return vConnection;}public void setConnection(Connection connection){ vConnection=connection;}public void setUseState(boolean bUseState){ this.bUseState=bUseState;}public boolean getUseState(){ return bUseState;}public void close(){ try { vConnection.close(); } catch(SQLException sqlException) { System.err.println(sqlException.getMessage()); }}}//连接池PoolBean.javapackage com.pool;import java.io.Serializable;import java.sql.*;import java.util.*;import com.pool.ConnectionBean;public class PoolBean implements java.io.Serializable{private String strDriver=null;private String strURL=null;private int iSize=0;private String strUserName="";private String strPassword="";private ConnectionBean vConnectionBean=null;private Vector vPool=null;public PoolBean(){}public void setDriver(String strDriver){ if(strDriver!=null) { this.strDriver=strDriver; }}public String getDriver(){ return strDriver;}public void setURL(String strURL){ if(strURL!=null) { this.strURL=strURL; }}public String getURL(){ return strURL;}public void setSize(int iSize){ if(iSize>1) { this.iSize=iSize; }}public int getSize(){ return iSize;}public String getUserName(){ return strUserName;}public void setUserName(String strUserName){ if(strUserName!=null) { this.strUserName=strUserName; }}public void setPassword(String strPassword){ if(strPassword!=null) { this.strPassword=strPassword; }}public String getPassword(){ return strPassword;}public void setConnectionBean(ConnectionBean vConnectionBean){ if(vConnectionBean!=null) { this.vConnectionBean=vConnectionBean; }}public ConnectionBean getConnectionBean() throws Exception{ Connection vConnection=getConnection(); ConnectionBean vConnectionBean=new ConnectionBean(vConnection); vConnectionBean.setUseState(true); return vConnectionBean;}private Connection createConnection() throws Exception{ Connection vConnection=null; vConnection=DriverManager.getConnection(strURL,strUserName,strPassword); return vConnection;}public synchronized void initializePool() throws Exception{ if(strDriver==null) { throw new Exception("没有提供驱动程序名称!"); } if(strURL==null) { throw new Exception("没有提供URL!"); } if(iSize<1) { throw new Exception("连接池大小小于1!"); } try { Class.forName(strDriver); for(int iIndex=0;iIndex<iSize;iIndex++) { Connection vConnection=createConnection(); if(vConnection!=null) { ConnectionBean vConnectionB
|