package chapter5;import java.sql.Connection;
import java.util.*;// Referenced classes of package chapter5:
//            DBConnPoolpublic class DBConnManager
{    private Vector poolnames;
    private Vector drivernames;
    private Vector dbids;
    private Vector usernames;
    private Vector passwds;
    private Vector maxconns;
    private Hashtable connPools;    public DBConnManager()
    {
        poolnames = new Vector();
        drivernames = new Vector();
        dbids = new Vector();
        usernames = new Vector();
        passwds = new Vector();
        maxconns = new Vector();
        connPools = new Hashtable();
        poolnames.addElement("sql");
        drivernames.addElement("com.microsoft.jdbc.sqlserver.SQLServerDriver");
        dbids.addElement("jdbc:microsoft:sqlserver://127.0.0.1:1038;DatabaseName=test");
        usernames.addElement("xiaohan");
        passwds.addElement("xiaohan");
        maxconns.addElement("10");
        createPools();
    }    public void releaseConnection(String s, Connection connection)
    {
        DBConnPool dbconnpool = (DBConnPool)connPools.get(s);
        if(dbconnpool != null)
            dbconnpool.releaseConnection(connection);
    }    public Connection getConnection(String s)
    {
        DBConnPool dbconnpool = (DBConnPool)connPools.get(s);
        if(dbconnpool != null)
            return dbconnpool.getConnection();
        else
            return null;
    }    public synchronized void closeConns()
    {
        DBConnPool dbconnpool;
        for(Enumeration enumeration = connPools.elements(); enumeration.hasMoreElements(); dbconnpool.closeConn())
            dbconnpool = (DBConnPool)enumeration.nextElement();    }    private void createPools()
    {
        for(int i = 0; i < poolnames.size(); i++)
        {
            String s = poolnames.elementAt(i).toString();
            String s1 = drivernames.elementAt(i).toString();
            String s2 = dbids.elementAt(i).toString();
            String s3 = usernames.elementAt(i).toString();
            String s4 = passwds.elementAt(i).toString();
            int j = 0;
            try
            {
                j = Integer.parseInt(maxconns.elementAt(i).toString());
            }
            catch(NumberFormatException numberformatexception)
            {
                numberformatexception.printStackTrace();
            }
            DBConnPool dbconnpool = new DBConnPool(s, s1, s2, s3, s4, j);
            connPools.put(s, dbconnpool);
        }    }
}//另一個.class ,這兩 個就可以通過JAVABEAN進行連接了
package chapter5;import java.sql.*;
import java.util.Enumeration;
import java.util.Vector;public class DBConnPool
{    private int inUse;
    private Vector connections;
    private String poolname;
    private String dbid;
    private String drivername;
    private String username;
    private String passwd;
    private int maxconn;    public DBConnPool(String s, String s1, String s2, String s3, String s4, int i)
    {
        inUse = 0;
        connections = new Vector();
        poolname = s;
        dbid = s2;
        drivername = s1;
        username = s3;
        passwd = s4;
        maxconn = i;
    }    public synchronized void releaseConnection(Connection connection)
    {
        connections.addElement(connection);
        inUse--;
    }    public synchronized Connection getConnection()
    {
        Connection connection = null;
        if(connections.size() > 0)
        {
            connection = (Connection)connections.elementAt(0);
            connections.removeElementAt(0);
            try
            {
                if(connection.isClosed())
                    connection = getConnection();
            }
            catch(SQLException sqlexception) { }
        } else
        if(maxconn == 0 || inUse < maxconn)
            connection = newConnection();
        if(connection != null)
            inUse++;
        return connection;
    }    private Connection newConnection()
    {
        Connection connection = null;
        try
        {
            Class.forName(drivername);
            connection = DriverManager.getConnection(dbid, username, passwd);
        }
        catch(Exception exception)
        {
            exception.printStackTrace();
            return null;
        }
        return connection;
    }    public synchronized void closeConn()
    {
        for(Enumeration enumeration = connections.elements(); enumeration.hasMoreElements();)
        {
            Connection connection = (Connection)enumeration.nextElement();
            try
            {
                connection.close();
            }
            catch(SQLException sqlexception)
            {
                sqlexception.printStackTrace();
            }
        }        connections.removeAllElements();
    }
}