谁能我数据库的连接池代码完整程序啊,我急用啊,谢谢各位! 已经发过去了,记得收啊里面有三个文件,分别是:.jar文件是连接池的,包里面也有源文件.properties是它的配置文件.jsp是一个连接数据库的例子 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.csdn.net/develop/article/19/19189.shtm ================ DBConnectionManager.java ==================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 DBConnectionManager implements Serializable{ /** * 连接池的实例 */ static private DBConnectionManager 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 DBConnectionManager(); 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 DBConnectionManager(); instance.init(); } instance.freeConn(con); } /** * ConnectionManager实例释放连接 */ private void freeConn(Connection con) { freeConnections.addElement(con); checkedOut--; } /** * 释放所有连接 */ /* public synchronized void release() { Enumeration allConnections = freeConnections.elements(); while (allConnections.hasMoreElements()) { Connection con = (Connection) allConnections.nextElement(); try { con.close(); }catch (SQLException e) { e.printStackTrace(); } } freeConnections = new Vector(); checkedOut = 0; }*/}///:~================== db.properties ==========================url=jdbc:mysql://150.0.1.111/beijing?useUnicode=true&charsetEncoding=ISO-8859-1driver=org.gjt.mm.mysql.Driveruser=easywebpassword=maxconn=60output=false 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 DBConnectionManager implements Serializable{ /** * 连接池的实例 */ static private DBConnectionManager 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 DBConnectionManager(); 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 DBConnectionManager(); instance.init(); } instance.freeConn(con); } /** * ConnectionManager实例释放连接 */ private void freeConn(Connection con) { freeConnections.addElement(con); checkedOut--; } /** * 释放所有连接 */ /* public synchronized void release() { Enumeration allConnections = freeConnections.elements(); while (allConnections.hasMoreElements()) { Connection con = (Connection) allConnections.nextElement(); try { con.close(); }catch (SQLException e) { e.printStackTrace(); } } freeConnections = new Vector(); checkedOut = 0; }*/}///:~================== db.properties ==========================url=jdbc:mysql://150.0.1.111/beijing?useUnicode=true&charsetEncoding=ISO-8859-1driver=org.gjt.mm.mysql.Driveruser=easywebpassword=maxconn=60output=false 谢谢,也给我一份吧!急用![email protected] 也给我一份吧!先谢谢了。[email protected] 也给我一份,谢谢[email protected] 拜托也给我一份!~ 谢谢了[email protected] thanks [email protected] 那位有的能否给一份?谢谢[email protected] jFreeChart 在tomcat temp目录下生成的临时文件 怎么处理啊? 求助~java获取网络资源问题 tomcat启动正常,网页打不开。着急!!! 才开始搞spring+strurs,问几个问题,希望能得到答案 需求电子商城源码 简单frame问题! 快来帮忙!!散分!!谢谢!! 求助!网页在线浏览功能,播放大点的CAD文件会导致adobe flash崩溃! 急!如何在jsp中实现把大图片转换成小图片? 请教关于用tomcat做连接池问题? 问一下关于application server的问题,路过的朋友看看 马上就用的问题!!!!!!!!!!!!
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 DBConnectionManager implements Serializable
{
/**
* 连接池的实例
*/
static private DBConnectionManager 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 DBConnectionManager();
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 DBConnectionManager();
instance.init();
}
instance.freeConn(con);
} /**
* ConnectionManager实例释放连接
*/
private void freeConn(Connection con)
{
freeConnections.addElement(con);
checkedOut--;
} /**
* 释放所有连接
*/
/*
public synchronized void release()
{
Enumeration allConnections = freeConnections.elements();
while (allConnections.hasMoreElements())
{
Connection con = (Connection) allConnections.nextElement();
try
{
con.close();
}catch (SQLException e)
{
e.printStackTrace();
}
}
freeConnections = new Vector();
checkedOut = 0;
}*/
}
///:~
================== db.properties ==========================url=jdbc:mysql://150.0.1.111/beijing?useUnicode=true&charsetEncoding=ISO-8859-1
driver=org.gjt.mm.mysql.Driver
user=easyweb
password=
maxconn=60
output=false
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 DBConnectionManager implements Serializable
{
/**
* 连接池的实例
*/
static private DBConnectionManager 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 DBConnectionManager();
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 DBConnectionManager();
instance.init();
}
instance.freeConn(con);
} /**
* ConnectionManager实例释放连接
*/
private void freeConn(Connection con)
{
freeConnections.addElement(con);
checkedOut--;
} /**
* 释放所有连接
*/
/*
public synchronized void release()
{
Enumeration allConnections = freeConnections.elements();
while (allConnections.hasMoreElements())
{
Connection con = (Connection) allConnections.nextElement();
try
{
con.close();
}catch (SQLException e)
{
e.printStackTrace();
}
}
freeConnections = new Vector();
checkedOut = 0;
}*/
}
///:~
================== db.properties ==========================url=jdbc:mysql://150.0.1.111/beijing?useUnicode=true&charsetEncoding=ISO-8859-1
driver=org.gjt.mm.mysql.Driver
user=easyweb
password=
maxconn=60
output=false
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
谢谢[email protected]