当前位置: 首页 > 学习 > 电脑学习 > 程序设计 > JAVA > 数据库 > 正文

一个通用的连接池Bean

http://www.zk168.com.cn  招考学习网 2006-4-11 2:05:15
-----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]--
一个通用的连接池Bean

//连接Bean

ConnectionBean.java


package com.pool;

import java.io.Serializable;
import java.sql.*;

public class ConnectionBean implements java.io.Serializable
{
private Connection vConnection=null;
private boolean bUseState=false;

public ConnectionBean(){}
public ConnectionBean(Connection connection)
{
if(connection!=null)
{
vConnection=connection;
}
}
public Connection getConnection()
{
return vConnection;
}
public void setConnection(Connection connection)
{
vConnection=connection;
}
public void setUseState(boolean bUseState)
{
this.bUseState=bUseState;
}
public boolean getUseState()
{
return bUseState;
}
public void close()
{
try
{
vConnection.close();
}
catch(SQLException sqlException)
{
System.err.println(sqlException.getMessage());
}
}
}


//连接池


PoolBean.java


package com.pool;

import java.io.Serializable;
import java.sql.*;
import java.util.*;
import com.pool.ConnectionBean;

public class PoolBean implements java.io.Serializable
{
private String strDriver=null;
private String strURL=null;
private int iSize=0;
private String strUserName="";
private String strPassword="";
private ConnectionBean vConnectionBean=null;
private Vector vPool=null;

public PoolBean(){}
public void setDriver(String strDriver)
{
if(strDriver!=null)
{
this.strDriver=strDriver;
}
}
public String getDriver()
{
return strDriver;
}
public void setURL(String strURL)
{
if(strURL!=null)
{
this.strURL=strURL;
}
}
public String getURL()
{
return strURL;
}
public void setSize(int iSize)
{
if(iSize>1)
{
this.iSize=iSize;
}
}
public int getSize()
{
return iSize;
}
public String getUserName()
{
return strUserName;
}
public void setUserName(String strUserName)
{
if(strUserName!=null)
{
this.strUserName=strUserName;
}
}
public void setPassword(String strPassword)
{
if(strPassword!=null)
{
this.strPassword=strPassword;
}
}
public String getPassword()
{
return strPassword;
}
public void setConnectionBean(ConnectionBean vConnectionBean)
{
if(vConnectionBean!=null)
{
this.vConnectionBean=vConnectionBean;
}
}
public ConnectionBean getConnectionBean() throws Exception
{
Connection vConnection=getConnection();
ConnectionBean vConnectionBean=new ConnectionBean(vConnection);
vConnectionBean.setUseState(true);
return vConnectionBean;
}
private Connection createConnection() throws Exception
{
Connection vConnection=null;
vConnection=DriverManager.getConnection(strURL,strUserName,strPassword);
return vConnection;
}
public synchronized void initializePool() throws Exception
{
if(strDriver==null)
{
throw new Exception("没有提供驱动程序名称!");
}
if(strURL==null)
{
throw new Exception("没有提供URL!");
}
if(iSize<1)
{
throw new Exception("连接池大小小于1!");
}
try
{
Class.forName(strDriver);
for(int iIndex=0;iIndex<iSize;iIndex++)
{
Connection vConnection=createConnection();
if(vConnection!=null)
{
ConnectionBean vConnectionBean=new ConnectionBean(vConnection);
addConnection(vConnectionBean);
}
}
}
catch(Exception eException)
{
System.err.println(eException.getMessage());
throw new Exception(eException.getMessage());
}
}
private void addConnection(ConnectionBean vConnectionBean)
{
if(vPool==null)
{
vPool=new Vector(iSize);
}
vPool.addElement(vConnectionBean);
}
public synchronized void releaseConnection(Connection vConnection)
{
for(int iIndex=0;iIndex<vPool.size();iIndex++)
{
ConnectionBean vConnectionBean=(ConnectionBean)vPool.elementAt(iIndex);
if(vConnectionBean.getConnection()==vConnection)
{
System.err.println("释放第"+iIndex+"个连接!");
vConnectionBean.setUseState(false);
break;
}
}
}
public synchronized Connection getConnection() throws Exception
{
ConnectionBean vConnectionBean=null;
for(int iIndex=0;iIndex<vPool.size();iIndex++)
{
vConnectionBean=(ConnectionBean)vPool.elementAt(iIndex);
if(vConnectionBean.getUseState()==false)
{
vConnectionBean.setUseState(true);
Connection vConnection=vConnectionBean.getConnection();
return vConnection;
}
}
try
{
Connection vConnection=createConnection();
vConnectionBean=new ConnectionBean(vConnection);
vConnectionBean.setUseState(true);
vPool.addElement(vConnectionBean);
}
catch(Exception eException)
{
System.err.println(eException.getMessage());
throw new Exception(eException.getMessage());
}
return vConnectionBean.getConnection();
}
public synchronized void emptyPool()
{
for(int iIndex=0;iIndex<vPool.size();iIndex++)
{
System.err.println("关闭第"+iIndex+"JDBC连接!");
ConnectionBean vConnectionBean=(ConnectionBean)vPool.elementAt(iIndex);
if(vConnectionBean.getUseState()==false)
{
vConnectionBean.close();
}
else
{
try
{
java.lang.Thread.sleep(20000);
vConnectionBean.close();
}
catch(InterruptedException interruptedException)
{
System.err.println(interruptedException.getMessage());
}
}
}
}
}
-----------------------------------------------------------[交流]-[打印]-[发送]-[收藏]--
最新入库:
 
·实质、过程及意义——阿多尔诺“否定的辩证法”探微
·从Ontology的译名之争看哲学术语的翻译原则
·论马克思主义哲学经典的解释——解释学方法及其在马克
·中国哲学当前的核心与周边问题
·和合学与21世纪文化价值和科技
·中国文化的和合精神与21世纪
·宗教之间理当相互宽容
·上半个世纪的自由主义
·殷周至春秋时期神人关系之演进
·大学之道:构建以“三纲八目”为核心的道德修养体系
相关内容:
 
·环保企业人力资源开发与管理的实证研究————巨龙公
·重油制气污水处理系统(A/O)技术改造
·BOG压缩机在液化石油气基地的应用
·英美CPA管理模式及其启示
·改造NERA微波公务信道为国产监控信道
·LFCB-102型微波分相差动保护的应用
·EAStudio让电子商务网站如虎添翼
·基于PB6和ORACLE8开发“劳动信息管理系统”
·巧解Pretty  Park 病毒一例
·利用TDC组件实现对WEB页面的交互操作
网友点评:
 
会员名称:
密码:匿名 ·注册·忘记密码?
评论内容:
(最多300个字符)
  查看评论
友情提醒:
 1.库中的资料大都来自互联网、网友上传、各类书籍,在录入的过程中难免会出现错误,恳请网
 友来信指正!
 2.如果网友在本库中未能找到所需要的材料,请登陆到我们的论坛《招考学习网》版块!
 3.考友想加入招考学习网的编辑部,请发信到XueXiWang#Gmail.com(#改为@)附带个人简历
 4.如需转载请注明出处及作者,谢谢合作!
 5.如果您有更好的建议或意见请EMAIL:XueXiWang#Gmail.com (#改为@)
 6.凡标题中有注有“[NO]”字样均不含答案且答案整理中.
 7.如本库中转载文章涉及版权等问题,请相关网站或作者在两周内发邮件通知(EMAIL:  XueXiWang#Gmail.com (#改为@))我们,我们接到通知后立即删除该文章及链接!
你问我答 更多>>