weblogic要免费,就没人用tomcat了,hehe
解决方案 »
- 有没有办法——获得当前类的引用?
- 2*17怎么运算他的效率最高啊
- AIX机器上的shell脚本如何调用java代码啊
- problem occured: Launching A(time of error 2008年。。。) 这是什么错误。。。
- 请教一个关于控制台下显示的问题
- 菜鸟问:如何指定JTree的一些节点可编辑(editable)而一些不好编辑
- 请问关于Toolkit.getDefaultToolkit().getScreenSize()的问题
- 关于Class
- 一个关于环境变量设置的问题
- 如何设置JTextArea里字体??通过什么方法可以实现?
- 初学者的作品,请多提意见。
- 各位大师,哪里可以找到JAVA的好书呀?
连接池:/*
* Author: song_qingjie
* Created: 02/19/2001 14:00:37
* Modified: 02/19/2001 14:00:37
*/import java.sql.*;
import java.util.*;
import javax.naming.*;public class ConnectionPool{
//最大连接数
private int maxConnection=1;
//已用连接数
private int boundConnNumber=0;
//驱动程序
private String DBDriver="sun.jdbc.odbc.JdbcOdbcDriver";
//数据源
private String ConnStr="jdbc:odbc:HET_ISSUEDB";
//用户名
private String UserName="sa";
//密码
private String Password=null;
//存放未用连接
private Vector freeConnections=null;
//存放已用连接
private Hashtable boundConnections=null;
//存放已建立的连接数
private int allConnectionNumber=0; public void setMaxConnection(int MaxConnection)
{
maxConnection=MaxConnection;
}
/*
* 设定驱动程序
*/
public void setDBDriver(String Driver)
{
DBDriver=Driver;
}
/*
* 设定数据库服务器地址
*/
public void setUrl(String Url)
{
ConnStr=Url;
}
/*
* 设定登陆用户名
*/
public void setUserName(String Username)
{
UserName=Username;
}
/*
* 设定登陆密码
*/
public void setPassword(String password)
{
Password=password;
}
/*
*加载驱动程序
*/
private void loadDriver()
{
//加载数据库驱动程序
try
{
Class.forName(DBDriver);
}
catch(Exception ex)
{
System.out.println("CreatePools error:"+ex.getMessage());
}
}
/**
*获得一个可用的连接,如果当前没有可用的连接并且已达最大连接数则等待,否则新建连接
*
* @return Connection 返回一个可用的连接
*/
public synchronized Connection getConnection()
{
//如果向量表中没有纪录则初始化并递归
try
{
//如果没有建立新连接则新建
if (freeConnections==null)
{
freeConnections=new Vector(maxConnection);
boundConnections=new Hashtable(maxConnection);
loadDriver();
newConnection();
}
//已建立的连接未达到最大连接并且没有空闲连接则新建
if ((allConnectionNumber<=maxConnection)&(freeConnections.size()==0))
newConnection();
//如果当前已经没有可用的连接,则等待知道有连接释放
if ((boundConnNumber>=maxConnection))
wait();
}
catch(InterruptedException ex)
{
System.out.println(ex.getMessage());
}
//已用连接加一
boundConnNumber++;
//取出一个空闲连接
Connection conn=(Connection)freeConnections.firstElement();
freeConnections.removeElement(conn);
boundConnections.put(Thread.currentThread(),conn);
notify();
return conn;
}
/*
*新建连接
*/
private void newConnection()
{
//如果已有连接没有达到最大连接数时获得一个新的连接并放在向量映射表中
if (++allConnectionNumber<=maxConnection)
{
try
{
freeConnections.addElement(DriverManager.getConnection(ConnStr,UserName,Password));
}
catch(SQLException ex)
{
System.out.println("Connection error:"+ex.getMessage());
}
}
}
/*
*释放当前连接
*/
public synchronized void returnConnection()
{
//获得当前使用的连接
Connection conn=(Connection)boundConnections.remove(Thread.currentThread());
if (conn!=null)
{
freeConnections.addElement(conn);
boundConnNumber--;
}
else
{
System.out.println("没有发现当前正在使用的连接");
}
notify();
}
/*
*关闭全部连接
*/
public void closeAllConnection()
{
try
{
if (boundConnections!=null)
{
for (Enumeration e=boundConnections.elements();e.hasMoreElements();)
{
Connection conn=(Connection)e.nextElement();
conn.close();
}
boundConnections.clear();
boundConnections=null;
}
if (freeConnections!=null)
{
for (Enumeration e=freeConnections.elements();e.hasMoreElements();)
{
Connection conn=(Connection)e.nextElement();
conn.close();
}
freeConnections.removeAllElements();
freeConnections=null;
}
}
catch(SQLException ex)
{
System.out.println(ex.getMessage());
}
}
}