package connection;
import java.io.Serializable;
import java.sql.*;
import java.util.*;
public class ConnPool implements java.io.Serializable{
private String driver = null; //数据库驱动程序对象
private String url = null; //数据源的位置
private int size = 0; //连接池的最大连接数目
private String username = ""; //数据源的用户名
private String password = ""; //数据源的密码
private DbConn dc=null;
private Vector pool = null; //连接池中的连接列表
public ConnPool(){}
//设置数据库驱动程序
public void setDriver(String driver){
if (driver!=null) this.driver=driver;
}
//获取数据库驱动程序
public String getDriver(){
return driver;
}
//设置数据源的位置
public void setURL(String url){
if (url!=null) this.url=url;
}
//获取数据源的位置
public String getURL(){
return url;
}
//设置最大连接数
public void setSize(int size){
if (size>1) this.size=size;
}
//获取最大连接数
public int getSize(){
return size;
}
//设置数据源的用户名
public void setUsername(String username){
if (username!=null) this.username=username;
}
//获取数据源的用户名
public String getUserName(){
return username;
}
//设置数据源的密码
public void setPassword(String password){
if (password!=null) this.password=password;
}
//获取数据源的密码
public String getPassword(){
return password;
}
//设置用于单个连接任务的DbConn对象
public void setConnBean(DbConn dc){
if (dc!=null) this.dc=dc;
}
//获取用于单个连接任务的DbConn对象
public DbConn getConnBean() throws Exception{
Connection conn = getConnection();
DbConn dc = new DbConn(conn); //实例化DbConn类
dc.setInuse(true); //设置此连接可用
return dc;
}
//创建到数据库的连接
private Connection createConnection() throws Exception{
Connection con = null;
con = DriverManager.getConnection(url,username,password);
return con;
}
//初始化连接池
public synchronized void initializePool() throws Exception{
if (driver==null) //如果没有加载驱动
throw new Exception("No Driver Provided!");
if (url==null) //如果没有设置数据源的位置
throw new Exception("No URL Proviced!");
if (size<1) //如果当前没有可用的连接
throw new Exception("Connection Pool Size is less than 1!");
try{
Class.forName(driver);
for (int i=0; i<size; i++){
//创建连接
Connection con = createConnection();
if (con!=null){
//将指定连接加入连接向量末尾
DbConn dc = new DbConn(con);
addConnection(dc);
}
}
}catch (Exception e){
System.err.println(e.getMessage());
throw new Exception(e.getMessage());
}
}
//将指定连接加入连接向量末尾
private void addConnection(DbConn conn){
if (pool==null) pool=new Vector(size);
pool.addElement(conn);
}
//释放指定连接的资源
public synchronized void releaseConnection(Connection con){
for (int i=0; i<pool.size(); i++){
DbConn connBean = (DbConn)pool.elementAt(i);
if (connBean.getConnection()==con){
//寻找到指定连接,将其置为未使用状态
connBean.setInuse(false);
break;
}
}
}
//从连接池得到一个连接
public synchronized Connection getConnection() throws Exception{
DbConn dc = null;
for (int i=0; i<pool.size(); i++){
dc = (DbConn)pool.elementAt(i); // 从连接列表中获得所有连接
if (dc.getInuse()==false){
//如果还有未使用的连接,则使用这个连接
dc.setInuse(true);
Connection con = dc.getConnection();
return con;
}
}
//如果连接都已使用,则新建一连接
try{
Connection con = createConnection();
dc = new DbConn(con);
dc.setInuse(true);
pool.addElement(dc);
}catch (Exception e){
System.err.println(e.getMessage());
throw new Exception(e.getMessage());
}
return dc.getConnection();
}
//清空连接池,释放资源
public synchronized void emptyPool(){
for (int i=0; i<pool.size(); i++){
DbConn connBean = (DbConn)pool.elementAt(i);
if (dc.getInuse()==false)
dc.close(); //释放连接资源
else{
try{
java.lang.Thread.sleep(20000);
dc.close();
}catch (InterruptedException ie){
System.err.println(ie.getMessage());
}
}
}
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
java毕业设计-JAVAWML信息查询与后端信息发布系统实现-WML信息查询设计(源代码+论文).zip (200个子文件)
200692013132532983.bmp 56KB
200691917242670274.bmp 56KB
20069201148066705.bmp 56KB
GetCode.bmp 1KB
ConnPool.class 4KB
DbConn.class 1KB
.classpath 584B
css.css 4KB
style.css 3KB
yu.css 327B
_WML信息查询与后端信息发布系统实现——WML信息查询设计.doc 485KB
menu_100.gif 12KB
menu_1000.gif 11KB
jayu1.gif 7KB
menu_1.gif 7KB
temp01.gif 5KB
ban01.gif 4KB
logo.gif 4KB
spss.gif 2KB
bg_shortcut01.gif 2KB
bg_08.gif 1KB
b_go.gif 1KB
b_go.gif 1KB
graph02.gif 1KB
graph03.gif 1KB
tjpro.gif 1KB
hotel3_bg2.gif 994B
bg_b.gif 884B
bg_b.gif 884B
hydl.gif 824B
2.gif 741B
4.gif 731B
e04.gif 688B
5.gif 680B
e03.gif 660B
fon_top02.gif 501B
im.gif 443B
guangbo.gif 352B
fon06.gif 337B
bot.gif 323B
dp_bg.gif 281B
fon_top01.gif 219B
top_sou.gif 216B
top_sq.gif 199B
top_cart.gif 193B
top_faq.gif 191B
fon03.gif 187B
fon01.gif 184B
fon04.gif 184B
fon02.gif 181B
bg_1.gif 175B
fon_top.gif 162B
bg_navBar01.gif 155B
allbg.gif 153B
icon03.gif 99B
icon01.gif 98B
icon02.gif 94B
bgbg.gif 63B
e01.gif 60B
e02.gif 60B
fon05.gif 58B
arrow.gif 58B
e_punct_b.gif 54B
wo.gif 54B
e_dot.gif 47B
px1.gif 43B
酒店咨询s.htm 70KB
error.html 417B
error.html 417B
msbase.jar 278KB
mssqlserver.jar 63KB
msutil.jar 58KB
ConnPool.java 4KB
DbConn.java 891B
27_risunki_122810.jpg 120KB
banner.jpg 58KB
jayu2.jpg 50KB
2006122215859097.jpg 45KB
20061221284085471.jpg 35KB
2006122291084256.jpg 30KB
200612271111983417.jpg 29KB
2006122233657781.jpg 29KB
main02.jpg 28KB
20061221514310332.jpg 26KB
20061221373528949.jpg 25KB
Img222951195.jpg 21KB
2006122310591492931.jpg 19KB
20061221443261485.jpg 18KB
20069161024551029.jpg 17KB
20069201193810998.jpg 17KB
20061291332440342.jpg 15KB
20069915545815501.jpg 8KB
pic.jpg 4KB
2.jpg 2KB
main01b.jpg 2KB
1.jpg 2KB
u=3188247578,3895638614&gp=14.jpg 1KB
main04.jpg 1KB
u=2078412773,2216325457&gp=34.jpg 1KB
main01.jpg 1KB
共 200 条
- 1
- 2
资源评论
易小侠
- 粉丝: 6465
- 资源: 9万+
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功