package test;
import java.sql.Connection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Vector;
public class DBConnManager {
//连接池名列表
private Vector<String> poolnames=new Vector<String>();
//驱动程序名列表
private Vector<String> drivernames=new Vector<String>();
//数据库标识列表
private Vector<String> dbids=new Vector<String>();
//用户名列表
private Vector<String> usernames=new Vector<String>();
//密码列表
private Vector<String> passwords=new Vector<String>();
//最大连接数列表
private Vector<String> maxconns=new Vector<String>();
//连接池队列
private Hashtable connPools=new Hashtable();
public DBConnManager(){
//添加sqlServer数据库连接信息
poolnames.addElement("sqlServer");
drivernames.addElement("sun.jdbc.odbc.JdbcOdbcDriver");
dbids.addElement("jdbc:odbc:TestData");
usernames.addElement("");
passwords.addElement("");
maxconns.addElement("10");
//添加mysql数据库的连接信息
poolnames.addElement("mysql");
drivernames.addElement("org.gjt.mm.mysql.Driver");
dbids.addElement("jdbc:mysql://localhost/testmysql");
usernames.addElement("root");
passwords.addElement("");
maxconns.addElement("10");
//添加access数据库的连接信息
poolnames.addElement("access");
drivernames.addElement("sun.jdbc.odbc.JdbcOdbcDriver");
dbids.addElement("jdbc:odbc:TestData");
usernames.addElement("");
passwords.addElement("");
maxconns.addElement("10");
//创建连接池
createPools();
}
//将连接返回给由指定的连接池
public void releaseConnection(String name,Connection con){
DBConnPool pool =(DBConnPool)connPools.get(name);
if(pool!=null)
pool.relaeaseConnection(con);
}
//得到一个指定连接池中的连接
public Connection getConnection(String name){
DBConnPool pool =(DBConnPool)connPools.get(name);
if(pool!=null)
return pool.getConnection();
return null;
}
//关闭所有连接
public synchronized void closeConns(){
Enumeration allPools=connPools.elements();
while(allPools.hasMoreElements()){
DBConnPool pool=(DBConnPool)allPools.nextElement();
pool.closeConn();
}
}
//创建连接池
@SuppressWarnings("unchecked")
private void createPools(){
for(int i=0;i<poolnames.size();i++){
String poolname=poolnames.elementAt(i).toString();
String drivername=drivernames.elementAt(i).toString();
String dbid=dbids.elementAt(i).toString();
String username=usernames.elementAt(i).toString();
String password=passwords.elementAt(i).toString();
int maxconn=0;
maxconn=Integer.parseInt(maxconns.elementAt(i).toString());
DBConnPool pool=new DBConnPool(poolname,drivername,dbid,username,password,maxconn);
connPools.put(poolname,pool);
}
}
}
package test;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Enumeration;
import java.util.Vector;
//连接池类,能够根据要求创建新连接,直到最大接连数为止
public class DBConnPool {
//实际使用中的连接数
private int inUse=0;
//空闲连接
private Vector<Connection> connections=new Vector<Connection>();
//连接池名
@SuppressWarnings("unused")
private String poolname;
//数据库标识
private String dbid;
//驱动程序名
private String drivername;
//数据库账号
private String username;
//数据库密码
private String password;
//最大连接数
private int maxconn;
public DBConnPool(String poolname,String drivername,String dbid,String username,String password,int maxconn){
this.poolname=poolname;
this.drivername=drivername;
this.dbid=dbid;
this.username=username;
this.password=password;
this.maxconn=maxconn;
}
//将连接返回给连接池
public synchronized void relaeaseConnection(Connection con){
//将指定连接加入到向量末尾
connections.addElement(con);
//连接数减一
inUse--;
}
//从连接池得到一个连接
public synchronized Connection getConnection(){
Connection con=null;
if(connections.size()>0){
//获取连接列表中获得第一个连接
con=(Connection)connections.elementAt(0);
connections.removeElementAt(0);
//如果此连接已关闭,则继续获取
try {
if(con.isClosed())
con=getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
}
//如果实际使用的连接小于最大连接数,就创建一个连接
else if(maxconn==0||inUse<maxconn){
con=newConnection();
}
if(con!=null){
//连接数增一
inUse++;
}
//返回一个连接
return con;
}
//创建新的连接
private Connection newConnection(){
Connection con =null;
try {
//加载驱动程序
Class.forName(drivername);
//建立连接
con=DriverManager.getConnection(dbid,username,password);
} catch (Exception e) {
e.printStackTrace();
return null;
}
//返回该连接
return con;
}
//关闭所有连接
public synchronized void closeConn(){
Enumeration allConnections=connections.elements();
while(allConnections.hasMoreElements()){
Connection con=(Connection)allConnections.nextElement();
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
connections.removeAllElements();
}
}