package com;
import java.sql.*;
import java.util.*;
import java.io.*;
import java.util.Vector;
import com.DBConnPool;
public class DBConnManager {
//连接池名列表
private Vector poolnames =new Vector();
//驱动程序名列表
private Vector drivernames=new Vector();
//数据库标识列表
private Vector dbids=new Vector();
//用户名列表
private Vector usernames=new Vector();
//用户密码列表
private Vector passwds=new Vector();
//最大连接数列表
private Vector maxconns=new Vector();
//连接池队列
private Hashtable connPools=new Hashtable();
public DBConnManager() {
//从配置文件读信息
try{
InputStream fis =getClass().getResourceAsStream("jdbcsql.properties");
Properties ps=new Properties();
ps.load(fis);
//连接Oracle8i/9i数据库
poolnames.addElement(ps.getProperty("databaseConnName0"));
drivernames.addElement(ps.getProperty("databaseConnDriver0"));
dbids.addElement(ps.getProperty("databaseConnUrl0"));
usernames.addElement(ps.getProperty("databaseConnUser0"));
passwds.addElement(ps.getProperty("databaseConnPwd0"));
maxconns.addElement(ps.getProperty("databaseConnMaxConns0"));
//添加Access数据库的连接信息
/* poolnames.addElement(ps.getProperty("Accesspoolnames"));
drivernames.addElement(ps.getProperty("Accessdrivernames"));
dbids.addElement(ps.getProperty("Accessdbids"));
usernames.addElement(ps.getProperty("Accessusernames"));
passwds.addElement(ps.getProperty("Accesspasswds"));
maxconns.addElement(ps.getProperty("Accessmaxconns"));
//添加SQL Server2000数据库的连接信息
poolnames.addElement(ps.getProperty("SQLpoolnames"));
drivernames.addElement(ps.getProperty("SQLdrivernames"));
dbids.addElement(ps.getProperty("SQLdbids"));
usernames.addElement(ps.getProperty("SQLusernames"));
passwds.addElement(ps.getProperty("SQLpasswds"));
maxconns.addElement(ps.getProperty("SQLmaxconns"));
//连接Mysql数据库信息
poolnames.addElement(ps.getProperty("Mysqlpoolnames"));
drivernames.addElement(ps.getProperty("Mysqldrivernames"));
dbids.addElement(ps.getProperty("Mysqldbids"));
usernames.addElement(ps.getProperty("Mysqlusernames"));
passwds.addElement(ps.getProperty("Mysqlpasswds"));
maxconns.addElement(ps.getProperty("Mysqlmaxconns")); */
//创建连接池
createPools();
} catch(Exception e){
System.out.println(e);
}
}
/*将连接返回给由指定的连接池*/
public void returnConnection(String name,Connection conn){
DBConnPool pool=(DBConnPool) connPools.get(name);
if (pool!=null) {
pool.returnConnection(conn);
}
}
/*得到一个指定连接池中的连接*/
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();
}
}
/*创建连接池*/
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 passwd=passwds.elementAt(i).toString();
int maxconn=0;
try{
maxconn=Integer.parseInt(maxconns.elementAt(i).toString());
}catch (NumberFormatException e){
e.printStackTrace();
}
DBConnPool pool=new DBConnPool(poolname,drivername,dbid,username,passwd,maxconn);
connPools.put(poolname,pool);
}
}
}
///////////////////DBConnPool 类////////////////////////////////
package com;
import java.sql.*;
import java.util.*;
import java.io.*;
import java.util.Vector;
public class DBConnPool {
// 正在使用连接的数量
private int using;
// 目前可用的连接数,即空闲连接
private Vector connections = new Vector();
// 最大连接数
private int maxconn;
// 连接池名
private String poolname;
// 数据库标识
private String dbid;
// 驱动程序名
private String drivername;
// 数据库帐号
private String username;
// 数据库密码
private String passwd;
public DBConnPool(String poolname, String drivername, String dbid,
String username, String passwd, int maxconn) {
this.poolname = poolname;
this.drivername = drivername;
this.dbid = dbid;
this.username = username;
this.passwd = passwd;
this.maxconn = maxconn;
}
/* 将空闲连接返回给连接池 */
public synchronized void returnConnection(Connection conn) {
// 将指定连接加到向量末尾
connections.addElement(conn);
// 正在使用连接的数量减一
using--;
}
/* 从连接池得到一个连接 */
public synchronized Connection getConnection() {
Connection conn = null; // Connection是一个类,
// connections是一个向量,用于存储连接对象,它所存储是的所有空闲状态的可用连接
if (connections.size() > 0) {
// 获取连接列表的第一个连接
conn = (Connection) connections.elementAt(0);
connections.removeElementAt(0);// 获得一个连接,并将此连接从队列中删除.
// 如果此连接已关闭,刚继续获取,
try {
if (conn.isClosed())
conn = getConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
// 如果实际使用的连接数小于最大连接数即有可用连接),就新增加一个连接
else if (maxconn == 0 || using < maxconn) {
// 如此时无可用连接(maxconn == 0)且连接数又未达到上限(using < maxconn)),就创建一个新连接
conn = newConnection();
}
// 如果连接数已达到上限就返回空指针
if (conn != null) {
using++;
}
return conn;
}
/* 创建新的连接 */
public Connection newConnection() {
Connection conn = null;
try {
// 加载驱动
Class.forName(drivername);
conn = DriverManager.getConnection(dbid, username, passwd);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return conn;
}
/* 关闭所有连接 */
public synchronized void closeConn() {
Enumeration allConnections = connections.elements();
while (allConnections.hasMoreElements()) {
Connection conn = (Connection) allConnections.nextElement();
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
connections.removeAllElements();
}
}
///////////////jdbcsql.properties配置文件,/////////////////////////
///////////////放在DBConnManager 类的CLASS文件的同一目录/////////
databaseConnectionCount =4
databaseConnName0=oracle
databaseConnDriver0=oracle.jdbc.driver.OracleDriver
databaseConnUrl0=jdbc:oracle:thin:@192.168.1.88:1521:hzshdb
databaseConnUser0=hzgps_taxi
databaseConnPwd0=hzjrdhzgps
databaseConnMaxConns0=10
databaseConnGeneralConns0=5
databaseConnName1=access
databaseConnDriver1=sun.jdbc.odbc.JdbcOdbcDriver
databaseConnUrl1=jdbc:odbc:
databaseConnUser1=
databaseConnPwd1=
databaseConnConns1=5
databaseConnName2=sqlserver2000
databaseConnDriver2=com.microsoft.jdbc.sqlserver.SQLServerDriver
databaseConnUrl2=jdbc:microsoft:sqlserver://localhost:1433;DatabserName=
databaseConnUser2=
databaseConnPwd2=
databaseConnConns2=5
databaseConnName3=mysql
databaseConnDriver3=org.gjt.mm.mysql.Driver
databaseConnUrl3=jdbc:mysql://localhost/shopping
databaseConnUser3=
databaseConnP