我写的一个数据库连接池的源代码,提宝贵意见者有分!

oicu 2003-05-16 04:36:20
package dbutil;

import java.io.*;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Date;
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;

/**
* 数据库连接池
*/
public class DBConnectionPool implements Serializable
{
/**
* 连接池的实例
*/
static private DBConnectionPool instance;

/**
* 正在被使用数据库连接个数
*/
private int checkedOut;

/**
* 保存空闲数据库连接的向量
*/
private Vector freeConnections;

/**
* 允许取得的最大的连接个数
*/
private int max;

/**
* 数据库密码
*/
private String password;

/**
* 数据库主机地址
*/
private String url;

/**
* 数据库用户名
*/
private String user;


/**
* 供外部调用的取得数据库连接的方法
* @param 可用的数据库连接
*/
public static synchronized Connection getConnection()
{
if (instance == null)
{
instance = new DBConnectionPool();
instance.init();
}
return instance.getConn();
}

/**
*读取配置文件内容
*/
private void init()
{
InputStream is = getClass().getResourceAsStream("./db.conf");
Properties dbProps = new Properties();
String filename = null;
freeConnections = new Vector();
try
{
dbProps.load(is);
}catch (Exception e)
{
System.err.println("Can not read config file!");
return;
}
url = dbProps.getProperty("url");
user = dbProps.getProperty("user","root");
password = dbProps.getProperty("password");
String maxConn = dbProps.getProperty("maxconn","50");
try
{
max = Integer.parseInt(maxConn);
}catch(Exception e){}
String driverClass = dbProps.getProperty("driver");
try
{
Object obj = Class.forName(driverClass).newInstance();
DriverManager.registerDriver((Driver)obj);
}catch(Exception e)
{
System.out.println("Can not load driver:"+driverClass);
}
}

/**
* 取得一个空闲连接或者新建一个连接
*/
private Connection getConn()
{
Connection con = null;
if(freeConnections.size() > 0)
{
con = (Connection) freeConnections.firstElement();
freeConnections.removeElementAt(0);
try
{
if (con.isClosed())
{
con = newConn();
}
}catch (SQLException e)
{
return null;
}
}else
{
if (checkedOut >= max)
{
return null;
}
con = newConn();
}
if (con != null)
{
checkedOut++;
}
return con;
}

/**
* 建立一个新的数据库连接
*/
private Connection newConn()
{
Connection con = null;
try
{
if (user == null)
{
con = DriverManager.getConnection(url);
}else
{
con = DriverManager.getConnection(url, user, password);
}
}catch (SQLException e)
{
e.printStackTrace();
return null;
}
return con;
}

/**
* 释放一个使用完了的数据库连接
*/
public static synchronized void freeConnection(Connection con)
{
if (instance == null)
{
instance = new DBConnectionPool();
instance.init();
}
instance.freeConn(con);
}

/**
* ConnectionManager实例释放连接
*/
private void freeConn(Connection con)
{
freeConnections.addElement(con);
checkedOut--;
}
}
///:~

附:
配置文件内容例子:
url=jdbc:mysql://localhost/testdb
driver=org.gjt.mm.mysql.Driver
user=root
password=
maxconn=60
output=false

使用方法:
...
Connection con = DBConnectionPool.getConnection();
...
DBConnectionPool.freeConnection(con);
...
...全文
114 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
老土豆T 2003-05-17
  • 打赏
  • 举报
回复
数据密码写在配置文件里。。对一个系统来说,这样是很不安全地:)


最好写个加密函数来EnCode 一下就好多啦:)
oicu 2003-05-17
  • 打赏
  • 举报
回复
说的好!多谢!

51,407

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧