求一个数据库连接池,高分相送 用Tomcat自己带的连接池。很好用的。去网上查一下。有很多介绍的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 http://www.google.com/search?q=java+%CA%FD%BE%DD%BF%E2+%C1%AC%BD%D3%B3%D8&hl=zh-CN&lr=lang_zh-CN&ie=gb2312 to all: 首先多谢各位的指点。 我的程序和应用服务器没有关系,自己写的连接池方便管理,而且以后可以用在任何地方。to orion11: 我已经收到你的程序,在看,谢谢。 楼主,能否我发一份,谢谢了![email protected] 楼主,给我一份,[email protected], Thanks a lot package hall;import java.util.HashMap;import java.util.Vector;import java.sql.Connection;import java.sql.DriverManager;import java.sql.*;/** * <p>Title: </p> * <p>Description: test</p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: </p> * @author xww */public class ConnectionPool { //连接池的管理器,首先初始化,仅仅有一个对象,管理连接池 private static HashMap connectionPoolManager = new HashMap(); //没有用过的连接池,用vector实现同步 private static Vector noUseConnectionPool= new Vector(); //没有用过的连接池 private static HashMap nowUseConnectionPool = new HashMap(); private static String dbDriver = "oracle.jdbc.driver.OracleDriver"; private static String dbUrl = "jdbc:oracle:thin:@localhost:1521:db"; private static String userName = "xww"; private static String userPassword = "11111111"; //默认为100个连接池 private static int MAX_POOL = 100; //singleTon 设计模式 private ConnectionPool(String driver, String url, String name, String password, int max) throws ClassNotFoundException { Class.forName(driver); dbUrl = url; userName = name; userPassword = password; MAX_POOL = max; } public static ConnectionPool getConnManagerInstance(String poolName) throws ClassNotFoundException { ConnectionPool tempPool = (ConnectionPool) connectionPoolManager.get( poolName); if (tempPool == null) { tempPool = new ConnectionPool(dbDriver, dbUrl, userName, userPassword, MAX_POOL); connectionPoolManager.put(poolName, tempPool); return tempPool; } else { return tempPool; } } //通过连接池获得真正的链接 public static Connection getConnection() throws java.sql.SQLException { Connection conn = null; synchronized (noUseConnectionPool) { if (noUseConnectionPool.size() > 0) { conn = (Connection) noUseConnectionPool.firstElement(); noUseConnectionPool.remove(conn); return conn; } } //如果数据库连接池没有链接了,自己创建一个 if (conn == null) { conn = createConnection(dbDriver, dbUrl, userName, userPassword); } else if (conn.isClosed()) { nowUseConnectionPool.remove(conn); conn = createConnection(dbDriver, dbUrl, userName, userPassword); } conn.setAutoCommit(false); nowUseConnectionPool.put(conn, conn); return conn; } //如果连接池没有链接了,就需要产生一个链接 private static Connection createConnection(String driver, String url, String user, String password) throws java.sql.SQLException { Connection conn = DriverManager.getConnection(url, user, password); return conn; } public static void releaseConnection(Connection conn, boolean isCommit) throws java.sql.SQLException { if (isCommit) { conn.commit(); } else { conn.rollback(); } nowUseConnectionPool.remove(conn); if (noUseConnectionPool.size() + nowUseConnectionPool.size() < MAX_POOL) { synchronized (noUseConnectionPool) { noUseConnectionPool.add(conn); } } else { conn.close(); } } public static void main(String[] args) { //测试模拟10个客户 for (int i = 0; i < 10; i++) { try { //xxxx 一般为属性文件读取 ConnectionPool pool = ConnectionPool.getConnManagerInstance("xxxx"); Connection conn = pool.getConnection(); } catch (SQLException ex1) { //处理异常 } catch (ClassNotFoundException ex) { //处理异常 } } }} 问一个问题 , 谁知道 Image对象截取其一部分,保存到Image对象中 java菜鸟问题 类与类之间的关系和操作? 安装Apache时 network domain 应该添什么 想不通的问题,请高手指教!!! 我才学5天这个问题难不倒你吧 有关使用final的一个问题 如何解多元一次方程组?(最多给300分以上!!) Application的入口问题 一个最最基本的问题 我要限制窗口最大化,应该怎么做!!!请大家帮帮忙!!!
http://www.google.com/search?q=java+%CA%FD%BE%DD%BF%E2+%C1%AC%BD%D3%B3%D8&hl=zh-CN&lr=lang_zh-CN&ie=gb2312
首先多谢各位的指点。
我的程序和应用服务器没有关系,自己写的连接池方便管理,而且以后可以用在任何地方。to orion11:
我已经收到你的程序,在看,谢谢。
[email protected]
import java.util.Vector;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.*;/**
* <p>Title: </p>
* <p>Description: test</p>
* <p>Copyright: Copyright (c) 2003</p>
* <p>Company: </p>
* @author xww
*/public class ConnectionPool { //连接池的管理器,首先初始化,仅仅有一个对象,管理连接池
private static HashMap connectionPoolManager = new HashMap();
//没有用过的连接池,用vector实现同步
private static Vector noUseConnectionPool= new Vector();
//没有用过的连接池
private static HashMap nowUseConnectionPool = new HashMap(); private static String dbDriver = "oracle.jdbc.driver.OracleDriver";
private static String dbUrl = "jdbc:oracle:thin:@localhost:1521:db";
private static String userName = "xww";
private static String userPassword = "11111111"; //默认为100个连接池
private static int MAX_POOL = 100; //singleTon 设计模式
private ConnectionPool(String driver, String url, String name,
String password, int max) throws
ClassNotFoundException {
Class.forName(driver);
dbUrl = url;
userName = name;
userPassword = password;
MAX_POOL = max;
} public static ConnectionPool getConnManagerInstance(String poolName) throws
ClassNotFoundException {
ConnectionPool tempPool = (ConnectionPool) connectionPoolManager.get(
poolName);
if (tempPool == null) {
tempPool = new ConnectionPool(dbDriver, dbUrl, userName, userPassword,
MAX_POOL);
connectionPoolManager.put(poolName, tempPool);
return tempPool;
}
else {
return tempPool;
}
} //通过连接池获得真正的链接
public static Connection getConnection() throws java.sql.SQLException {
Connection conn = null;
synchronized (noUseConnectionPool) {
if (noUseConnectionPool.size() > 0) {
conn = (Connection) noUseConnectionPool.firstElement();
noUseConnectionPool.remove(conn);
return conn;
}
}
//如果数据库连接池没有链接了,自己创建一个
if (conn == null) {
conn = createConnection(dbDriver, dbUrl, userName, userPassword);
}
else if (conn.isClosed()) {
nowUseConnectionPool.remove(conn);
conn = createConnection(dbDriver, dbUrl, userName, userPassword);
}
conn.setAutoCommit(false);
nowUseConnectionPool.put(conn, conn);
return conn;
} //如果连接池没有链接了,就需要产生一个链接
private static Connection createConnection(String driver, String url,
String user, String password) throws
java.sql.SQLException {
Connection conn = DriverManager.getConnection(url, user, password);
return conn;
} public static void releaseConnection(Connection conn, boolean isCommit) throws
java.sql.SQLException {
if (isCommit) {
conn.commit();
}
else {
conn.rollback();
}
nowUseConnectionPool.remove(conn);
if (noUseConnectionPool.size() + nowUseConnectionPool.size() < MAX_POOL) {
synchronized (noUseConnectionPool) {
noUseConnectionPool.add(conn);
}
}
else {
conn.close();
}
} public static void main(String[] args) {
//测试模拟10个客户
for (int i = 0; i < 10; i++) {
try {
//xxxx 一般为属性文件读取
ConnectionPool pool = ConnectionPool.getConnManagerInstance("xxxx");
Connection conn = pool.getConnection(); }
catch (SQLException ex1) {
//处理异常
}
catch (ClassNotFoundException ex) {
//处理异常
}
}
}
}