第一个:数据库实体类
package com.ryan.configbean;
public class DSConfigBean {
private String type =""; //数据库类型
private String name =""; //连接池名字
private String driver =""; //数据库驱动
private String url =""; //数据库url
private String username =""; //用户名
private String password =""; //密码
private int maxconn =0; //最大连接数
/**
*
*/
public DSConfigBean() {
// TODO Auto-generated constructor stub
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getMaxconn() {
return maxconn;
}
public void setMaxconn(int maxconn) {
this.maxconn = maxconn;
}
}
第二个:数据库连接池
package com.ryan.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
public class DBConnectionPool {
private Connection con=null;
private int inUsed=0; //使用的连接数
private ArrayList freeConnections = new ArrayList();//容器,空闲连接
private int minConn; //最小连接数
private int maxConn; //最大连接
private String name; //连接池名字
private String password; //密码
private String url; //数据库连接地址
private String driver; //驱动
private String user; //用户名
/**
*
*/
public DBConnectionPool() {
// TODO Auto-generated constructor stub
}
/**
* 创建连接池
* @param driver
* @param name
* @param URL
* @param user
* @param password
* @param maxConn
*/
public DBConnectionPool(String name, String driver,String URL, String user, String password, int maxConn)
{
this.name=name;
this.driver=driver;
this.url=URL;
this.user=user;
this.password=password;
this.maxConn=maxConn;
}
/**
* 用完,释放连接
* @param con
*/
public synchronized void freeConnection(Connection con)
{
this.freeConnections.add(con);//添加到空闲连接的末尾
this.inUsed--;
}
/**
* timeout 根据timeout得到连接
* @param timeout
* @return
*/
public synchronized Connection getConnection(long timeout)
{
Connection con=null;
if(this.freeConnections.size()>0)
{
con=(Connection)this.freeConnections.get(0);
if(con==null)con=getConnection(timeout); //继续获得连接
}
else
{
con=newConnection(); //新建连接
}
if(this.maxConn==0||this.maxConn<this.inUsed)
{
con=null;//达到最大连接数,暂时不能获得连接了。
}
if(con!=null)
{
this.inUsed++;
}
return con;
}
/**
*
* 从连接池里得到连接
* @return
*/
public synchronized Connection getConnection()
{
Connection con=null;
if(this.freeConnections.size()>0)
{
con=(Connection)this.freeConnections.get(0);
this.freeConnections.remove(0);//如果连接分配出去了,就从空闲连接里删除
if(con==null)con=getConnection(); //继续获得连接
}
else
{
con=newConnection(); //新建连接
}
if(this.maxConn==0||this.maxConn<this.inUsed)
{
con=null;//等待 超过最大连接时
}
if(con!=null)
{
this.inUsed++;
System.out.println("得到 "+this.name+" 的连接,现有"+inUsed+"个连接在使用!");
}
return con;
}
/**
*释放全部连接
*
*/
public synchronized void release()
{
Iterator allConns=this.freeConnections.iterator();
while(allConns.hasNext())
{
Connection con=(Connection)allConns.next();
try
{
con.close();
}
catch(SQLException e)
{
e.printStackTrace();
}
}
this.freeConnections.clear();
}
/**
* 创建新连接
* @return
*/
private Connection newConnection()
{
try {
Class.forName(driver);
con=DriverManager.getConnection(url, user, password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("sorry can't find db driver!");
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
System.out.println("sorry can't create Connection!");
}
return con;
}
public int getInUsed() {
return inUsed;
}
public void setInUsed(int inUsed) {
this.inUsed = inUsed;
}
public int getMinConn() {
return minConn;
}
public void setMinConn(int minConn) {
this.minConn = minConn;
}
public int getMaxConn() {
return maxConn;
}
public void setMaxConn(int maxConn) {
this.maxConn = maxConn;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getDriver() {
return driver;
}
public void setDriver(String driver) {
this.driver = driver;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
}
第三个:数据库连接池管理
package com.ryan.db;
import java.sql.Connection;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Vector;
import com.ryan.configbean.DSConfigBean;
public class DBConnectionManager {
static private DBConnectionManager instance;//唯一数据库连接池管理实例类
static private int clients; //客户连接数
private Vector drivers = new Vector();//驱动信息
private Hashtable pools=new Hashtable();//连接池
/**
* 实例化管理类
*/
public DBConnectionManager() {
// TODO Auto-generated constructor stub
// this.init();
}
/**
* 得到唯一实例管理类
* @return
*/
static synchronized public DBConnectionManager getInstance()
{
if(instance==null)
{
instance=new DBConnectionManager();
}
return instance;
}
/**
* 释放连接
* @param name
* @param con
*/
public void freeConnection(String name, Connection con)
{
DBConnectionPool pool=(DBConnectionPool)pools.get(name);//根据关键名字得到连接池
if(pool!=null)
pool.freeConnection(con);//释放连接
}
/**
* 得到一个连接根据连接池的名字name
* @param name
* @return
*/
public Connection getConnection(String name)
{
DBConnectionPool pool=null;
Connection con=null;
pool=(DBConnectionPool)pools.get(name);//从名字中获取连接池
con=pool.getConnection();//从选定的连接池中获得连接
if(con!=null)
System.out.println("得到连接。。。");
return con;
}
/**
* 得到一个连接,根据连接池的名字和等待时间
* @param name
* @param time
* @return
*/
public Connection getConnection(String name, long timeout)
{
DBConnectionPool pool=null;
Connection con=null;
pool=(DBConnectionPool)pools.get(name);//从名字中获取连接池
con=pool.getConnection(timeout);//从选定的连接池中获得连接
System.out.println("得到连接。。。");
return con;
}
/**
* 释放所有连接
*/
public synchronized void release()
{
Enumeration allpools=pools.elements();
while(allpools.hasMoreElements())
{
DBConnectionPool pool=(DBConnectionPool)allpools.nextElement();
if(pool!=null)pool.release();
}
pools.clear();
}
/**
* 创建连接池
* @param props
*/
private void createPools(DSConfigBean dsb)
{
DBConnectionPool dbpool = (DBConnectionPool)pools.get(dsb.getName());
if(dbpool==null){
DBConnectionPool dbpool1=new DBConnectionPool();
dbpool1.setName(dsb.getName());
dbpool1.setDriver(dsb.getDriver());
dbpool1.setUrl(dsb.getUrl());
dbpool1.setUser(dsb.getUsername());
dbpool1.setPassword(dsb.getPassword());
dbpool1.setMaxConn(dsb.getMaxconn());
System.out.println("ioio:"+dsb.getMaxconn());
pools.put(dsb.getName(), dbpool1);
}
}
/**
* 初始化连接池的参数
*/
public void init(DSConfigBean dscBean)
{
//加载驱动程序
// this.loadDrivers();
//创建连接池
System.out.println("创建连接池。。。");
this.createPools(dscBean);
System.out.println("创建连接池完毕。。。");
}
public DSConfigBean loadDrivers(String name){
DSConfigBean dscBean=new DSConfigBean();
dscBean.setType("oracle");
dscBean.setName(name);
dscBean.setDriver("oracle.jdbc.driver.OracleDriver");
dscBean.setUrl("jdbc:oracl:thin:@192.168.1.88:1521:orcl");
dscBean.setUsername("dwgj");
dscBean.setPassword("dwgj116");
dscBean.setMaxconn(1000);
return dscBean;
}
public static void main(String []args){
DBConnectionManager connectionMan=DBConnectionManager .getInstance();//得
//得到连接
String name="oracle";//从上下文得到你要访问的数据库的名字
Connection con=connectionMan.getConnection(name);
connectionMan.freeConnection(name,con);//释放,但并未断开连接
}
}