当前位置:七道奇文章资讯编程技术Java编程
日期:2011-03-22 16:16:00  来源:本站整理

一个通用的衔接池Bean[Java编程]

赞助商链接



  本文“一个通用的衔接池Bean[Java编程]”是由七道奇为您精心收集,来源于网络转载,文章版权归文章作者所有,本站不对其观点以及内容做任何评价,请读者自行判断,以下是其具体内容:

//衔接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());
   }
   }
  }
 }
}

用JSDK141下编译通过


  以上是“一个通用的衔接池Bean[Java编程]”的内容,如果你对以上该文章内容感兴趣,你可以看看七道奇为您推荐以下文章:
  • 一个通用的衔接池Bean
  • 本文地址: 与您的QQ/BBS好友分享!
    • 好的评价 如果您觉得此文章好,就请您
        0%(0)
    • 差的评价 如果您觉得此文章差,就请您
        0%(0)

    文章评论评论内容只代表网友观点,与本站立场无关!

       评论摘要(共 0 条,得分 0 分,平均 0 分) 查看完整评论
    Copyright © 2020-2022 www.xiamiku.com. All Rights Reserved .