www.pudn.com > Easy_Buy电子商务系统.rar > PoolBean.java


//package eshop; 
import java.sql.*; 
import java.util.*; 
 
public class PoolBean { 
  private int inUse=0;	//实际使用中的连接数	 
  private int maxconn;	//连接池支持的最大连接数 
  private Vector connections=new Vector();	 
  private String poolMessage;	//连接池的状态 
  private String driver;	//数据库驱动程序	 
  private String url;		//数据库URL 
  private String username;	//数据库用户名 
  private String password;	//数据库密码 
 
 
  /*构造函数*/ 
  public PoolBean() { 
    //this.driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; 
    //this.url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=eshop"; 
    this.driver="sun.jdbc.odbc.JdbcOdbcDriver"; 
    this.url="jdbc:odbc:MyDataSource"; 
    this.username="sa"; 
    this.password=""; 
    this.maxconn=100; 
    this.poolMessage="构造成功!"; 
  } 
 
  /*将连接返回给连接池*/ 
  public synchronized void releaseConnection(Connection conn){ 
    connections.addElement(conn); 
    inUse--; 
  } 
 
  /*从连接池得到一个连接*/ 
  public synchronized Connection getConnection(){ 
    Connection conn=null; 
    if(connections.size()>0){ 
      conn=(Connection)connections.elementAt(0); 
      connections.removeElementAt(0); 
      try{ 
        if(conn.isClosed()){ 
          conn=getConnection(); 
        } 
      } 
      catch(Exception e){ 
        e.printStackTrace(); 
        poolMessage="错误!试图获取连接时出错!"; 
      } 
    } 
    else if(maxconn==0||inUse