⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connpool.java

📁 本例讲述了如何使用JSP技术编写论坛系统
💻 JAVA
字号:
package bbs;

import java.sql.*;
import java.util.*;

public class ConnPool {
	private int ConnNow=0;
	private Vector connections = new Vector();
	private String PoolName;
	private String DriverName;
	private String DbId;
	private String UserName;
	private String Password;
	private int MaxConn;

	public ConnPool(String PoolName, String DriverName, String DbId, String UserName, String Password, int MaxConn) {
		this. PoolName = PoolName;
		this. DriverName = DriverName;
		this. DbId = DbId;
		this. UserName = UserName;
		this. Password = Password;
		this. MaxConn = MaxConn;
	}

	public synchronized void releaseConnection(Connection conn) {
		connections.addElement(conn);
		ConnNow--;
	}

	public synchronized Connection getConnection() {
		Connection conn = null;
		if (connections.size() > 0) {
			conn = (Connection) connections.elementAt(0);
			connections.removeElementAt(0);
         try {
            if (conn.isClosed())
               conn = getConnection();
         }
         catch (Exception ex) {
            ex.printStackTrace();
         }
		}
		else if (MaxConn == 0 || ConnNow <MaxConn) {
			conn = newConnection();
		}
		if (conn != null) {
			ConnNow++;
		}
		return conn;
	}


	private Connection newConnection() {
		Connection conn = null;
		try {
			Class.forName(DriverName);
			conn = DriverManager.getConnection(DbId,UserName, Password);
		}
		catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return conn;
	}

	public synchronized void closeConn() {
		Enumeration allConnections = connections.elements();
		while (allConnections.hasMoreElements()) {
			Connection conn = (Connection) allConnections.nextElement();
			try {
				conn.close();
			}
			catch (SQLException e) {
				e.printStackTrace();
			}
		}
		connections.removeAllElements();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -