//連接Bean ConnectionBean.Java
package 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.Java
package 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 vConnectionBean=new ConnectionBean(vConnection);
addConnection(vConnectionBean);
}
}
}
catch(Exception eException)
{
System.err.println(eException.getMessage());
throw new Exception(eException.getMessage());
}
}
private void addConnection(ConnectionBean vConnectionBean)
{
if(vPool==null)
{
vPool=new Vector(iSize);
}
vPool.addElement(vConnectionBean);
}
public synchronized void releaseConnection(Connection vConnection)
{
for(int iIndex=0;iIndex<vPool.size();iIndex++)
{
ConnectionBean vConnectionBean=(ConnectionBean)vPool.elementAt(iIndex);
if(vConnectionBean.getConnection()==vConnection)
{
System.err.println("釋放第"+iIndex+"個連接!");
vConnectionBean.setUseState(false);
break;
}
}
}
public synchronized Connection getConnection() throws Exception
{
ConnectionBean vConnectionBean=null;
for(int iIndex=0;iIndex<vPool.size();iIndex++)
{
vConnectionBean=(ConnectionBean)vPool.elementAt(iIndex);
if(vConnectionBean.getUseState()==false)
{
vConnectionBean.setUseState(true);
Connection vConnection=vConnectionBean.getConnection();
return vConnection;
}
}
try
{
Connection vConnection=createConnection();
vConnectionBean=new ConnectionBean(vConnection);
vConnectionBean.setUseState(true);
vPool.addElement(vConnectionBean);
}
catch(Exception eException)
{
System.err.println(eException.getMessage());
throw new Exception(eException.getMessage());
}
return vConnectionBean.getConnection();
}
public synchronized void emptyPool()
{
for(int iIndex=0;iIndex<vPool.size();iIndex++)
{
System.err.println("關閉第"+iIndex+"JDBC連接!");
ConnectionBean vConnectionBean=(ConnectionBean)vPool.elementAt(iIndex);
if(vConnectionBean.getUseState()==false)
{
vConnectionBean.close();
}
else
{
try
{
Java.lang.Thread.sleep(20000);
vConnectionBean.close();
}
catch(InterruptedException interruptedException)
{
System.err.println(interruptedException.getMessage());
}
}
}
}
}
用JSDK141下編譯通過