首 页 网络编程
网页制作 图形图象 操作系统 冲浪宝典
软件教学 认证考试

网络安全 网络办公 行业资讯 评测对比
您当前位置:站长天空 -> 操作系统
一个通用的连接池Bean-JSP教程,资料/其它
作者:网友供稿 点击:22
推荐
西部数码-全国虚拟主机10强!20余项虚拟主机管理功能,全国领先!第6代双线路虚拟主机,南北访问畅通无阻!可在线rar解压,自动数据恢复设置虚拟目录等.免费赠送访问统计,企业邮局.Cn域名注册10元/年,自助建站480元起,免费试用7天,满意再付款!P4主机租用799元/月.月付免压金
站内搜索
文章页数:[1] 
//连接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());
}
}
}
}
}


文章整理:站长天空 网址:http://www.z6688.com/
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

文章页数:[1] 


放大字体显示 缩小字体显示 打印文章 推荐给朋友
热门文章
·在.Net中嵌入资源文件到程序集中-.NET教程,VB.Net语言
·字符串转换成十六进制-.NET教程,算法/线程
·获得网卡MAC地址和IP地址(VB.net)-.NET教程,VB.Net语言
·.Net框架程序设计(一)----进阶-.NET教程,.NET Framework
·我是否可以将Excel数据导入?-ASP教程,数据库相关
·封面的最佳设计-.NET教程,评论及其它
·使用C#控制远程计算机的服务-.NET教程,C#语言
·利用XMLHTTP下载文件-ASP教程,XML相关
·关于VB.net中无法使用VB6控件-.NET教程,VB.Net语言
·.NET里面的Interop太烂了-.NET教程,.NET Framework
最新文章
·windowsxp sp3 概览里面到底有什么?_windows xp
·让vista系统停止讨厌的报告程序问题_windows vista
·如果你有100万流量如何赚钱_网赚技巧
·携程海归派ceo梁建章:天才整合旅游业_站长访谈
·狂热+坚持=成功 博客园ceo dudu访谈_站长访谈
·php5对mysql5的任意数据库表的管理代码示例(三) _php技巧
·php5对mysql5的任意数据库表的管理代码示例(一)_php技巧
·好的产品才是最好的推广_站长心得
·视频网站的媒体传播现状走向_站长心得
·点评一下目前国内各站长类网站发展现状_站长心得
相关主题
  • 一个通用的分页类_asp.net技巧
  • 一个通用的表单验证程序-网页设计,HTML/CSS
  • 一个通用的保护ASP系统的方法-ASP教程,安全加密
  • 一个通用的Datagrid导出Excel打印的源函数-.NET教程,数据库应用
  • 一个通用的JSP分页程序-JSP教程,Jsp/Servlet
  • 西部数码虚拟主机

    友情链接
    CNNIC 西部数码
    万网 自助建站
    虚拟主机 asp空间
    域名注册 域名
    域名申请 主页空间
    论坛空间 网站空间
    国际域名 虚拟空间
    空间租用 DDOS防火墙
    成都主机托管 四川主机托管
    主机租用 服务器租用
    网站目录 自助建站
    虚拟主机 网址大全
    软件下载
    自助链接
    虚拟主机资讯 特价虚拟主机
    版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
    关于我们:站长天空:专业提供最新的站长资讯、在线教程、虚拟主机权威评测、虚拟主机性能对比、网站制作教程,开发教程,站长工具。包括网页制作教程、冲浪宝典、编程参考、操作系统、软件教学、行业动态等。
    特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。
    发表评论 打印  刷新     关闭