连接池的实现

IDBConnection.java

package org.lbr.sqlserver;  
import java.sql.Connection;  
import java.sql.SQLException;  
public interface IDBConnection {  
    public Connection createConnection() throws ClassNotFoundException,  
            SQLException;  
    public void closeConn() throws SQLException;  
    public Connection getConn();  
}  

IDBConnectionManager.java

package org.lbr.sqlserver;  
import java.sql.SQLException;  
public interface IDBConnectionManager {  
    public IDBConnection getAvailableConnection() throws ClassNotFoundException,  
            SQLException;  
    public void closeConn(IDBConnection dbConnection) throws SQLException;  
}  

SQLConnection.java

package org.lbr.sqlserver;  
import java.io.IOException;  
import java.sql.Connection;  
import java.sql.DriverManager;  
import java.sql.SQLException;  
public class SQLConnection implements IDBConnection {  
    private static SQLServerProperties prop = new SQLServerProperties();  
    private Connection conn;  
    public Connection createConnection() throws ClassNotFoundException,  
            SQLException {  
        try {  
            Class.forName(prop.getDBDRIVER());  
            conn = DriverManager.getConnection(prop.getDBURL(), prop  
                    .getDBUSER(), prop.getDBPASS());  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return conn;  
    }  
    public void closeConn() throws SQLException {  
        if (!conn.isClosed()) {  
            conn.close();  
            conn = null;  
        }  
    }  
    public Connection getConn() {  
        return conn;  
    }  
}  

SQLConnectionManager.java

package org.lbr.sqlserver;  
import java.sql.Connection;  
import java.sql.SQLException;  
public class SQLConnectionManager implements IDBConnectionManager {  
    private static SQLConnectionManager dbConnection;  
    private static final int connectionSize = 5;  
    private SQLConnection[] connections;  
    private SQLConnectionManager() {  
        connections = new SQLConnection[connectionSize];  
    }  
    public static SQLConnectionManager getSQLServerConnectionManagerInstance() {  
        if (dbConnection == null) {  
            dbConnection = new SQLConnectionManager();  
        }  
        return dbConnection;  
    }  
    public IDBConnection getAvailableConnection() throws ClassNotFoundException,  
            SQLException {  
        SQLConnection sqlConnection = null;  
        for (int i = 0; i < connectionSize; i++) {  
            if (connections[i] != null) {  
                if (connections[i].getConn() == null) {  
                    sqlConnection = connections[i];  
                    System.out.print("找到位置:" + i + "\t");  
                    break;  
                }  
            }  
        }  
        if (sqlConnection == null) {  
            for (int i = 0; i < connectionSize; i++) {  
                if (connections[i] == null) {  
                    sqlConnection = new SQLConnection();  
                    connections[i] = sqlConnection;  
                    System.out.print("找到位置:" + i + "\t");  
                    break;  
                }  
            }  
        }  
        return sqlConnection;  
    }  
    public void closeConn(IDBConnection dbConnection) throws SQLException {  
        dbConnection.closeConn();  
    }  
    public static void main(String[] args) {  
        SQLConnectionManager dbConnectionManager = SQLConnectionManager  
                .getSQLServerConnectionManagerInstance();  
        try {  
            // DBConnection dbConnection =  
            // dbConnectionManager.getAvailableConnection();  
            // Connection conn = dbConnection.createConnection();  
            // System.out.println(conn.isClosed());  
            // dbConnectionManager.closeConn(dbConnection);  
            IDBConnection[] dbConnection = new IDBConnection[10];  
            for (int i = 0; i < 7; i++) {  
                dbConnection[i] = dbConnectionManager.getAvailableConnection();  
                if (dbConnection[i] == null) {  
                    System.out.print("创建第 " + i + " 个对象 : 没有剩余空间,正在寻找可用空间");  
                    dbConnectionManager.closeConn(dbConnection[i - 3]);  
                    System.out.print("\t关闭了空间" + (i - 3) + ",再次寻找可用空间\t");  
                    dbConnection[i] = dbConnectionManager  
                            .getAvailableConnection();  
                }  
                Connection conn = dbConnection[i].createConnection();  
                System.out.print("创建第 " + i + " 个对象 : " + conn.isClosed());  
                // dbConnectionManager.closeConn(dbConnection[i]);  
                System.out.println("\t是否关闭了:" + conn.isClosed());  
            }  
        } catch (ClassNotFoundException e) {  
            e.printStackTrace();  
        } catch (SQLException e) {  
            e.printStackTrace();  
        }  
    }  
}  

SQLServerProperties.java

package org.lbr.sqlserver;  
import java.io.IOException;  
import java.io.InputStream;  
import java.util.Properties;  
public class SQLServerProperties {  
    private String DBDRIVER;  
    private String DBURL;  
    private String DBUSER;  
    private String DBPASS;  
    public String getDBDRIVER() throws ClassNotFoundException, IOException {  
        if (DBDRIVER == null) {  
            DBDRIVER = this.getProperties("DBDRIVER");  
        }  
        return DBDRIVER;  
    }  
    public void setDBDRIVER(String dbdriver) {  
        DBDRIVER = dbdriver;  
    }  
    public String getDBURL() throws ClassNotFoundException, IOException {  
        if (DBURL == null) {  
            DBURL = this.getProperties("DBURL");  
        }  
        return DBURL;  
    }  
    public void setDBURL(String dburl) {  
        DBURL = dburl;  
    }  
    public String getDBUSER() throws ClassNotFoundException, IOException {  
        if (DBUSER == null) {  
            DBUSER = this.getProperties("DBUSER");  
        }  
        return DBUSER;  
    }  
    public void setDBUSER(String dbuser) {  
        DBUSER = dbuser;  
    }  
    public String getDBPASS() throws ClassNotFoundException, IOException {  
        if (DBPASS == null) {  
            DBPASS = this.getProperties("DBPASS");  
        }  
        return DBPASS;  
    }  
    public void setDBPASS(String dbpass) {  
        DBPASS = dbpass;  
    }  
    private String getProperties(String key) throws ClassNotFoundException,  
            IOException {  
        InputStream in = null;  
        Properties p = new Properties();  
        in = Class.forName("org.lbr.sqlserver.SQLServerProperties")  
                .getResourceAsStream("/conf/sqlserverproperties.properties");  
        p.load(in);  
        return p.getProperty(key);  
    }  
    public static void main(String[] args) {  
        SQLServerProperties dbProperties = new SQLServerProperties();  
        try {  
            System.out.println(dbProperties.getDBDRIVER());  
        } catch (ClassNotFoundException e) {  
            e.printStackTrace();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
    }  
}  

sqlserverproperties.properties

DBDRIVER=com.microsoft.sqlserver.jdbc.SQLServerDriver  
DBURL=jdbc:sqlserver://localhost:1433; DatabaseName=Test  
DBUSER=sa  
DBPASS=1225

连接池的简单实现,完.