本例讲述了如何使用JSP技术编写在线商务沟通系统

源代码在线查看: connpool.java

软件大小: 130 K
上传用户: z147028571a
关键词: JSP 如何使用 编写
下载地址: 免注册下载 普通下载 VIP

相关代码

				package bbs;
				
				import java.sql.*;
				import java.util.*;
				
				public class ConnPool {
					private int ConNow=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, intMaxConn) {
						this. PoolName = PoolName;
						this. DriverName = DriverName;
						this. DbId = DbId;
						this. UserName = UserName;
						this. Password = Password;
						this. MaxConn = MaxConn;
					}
				
					public synchronized void releaseConnection(Connection con) {
						connections.addElement(con);
						ConNow--;
					}
				
					public synchronized Connection getConnection() {
						Connection con = null;
						if (connections.size() > 0) {
							con = (Connection) connections.elementAt(0);
							connections.removeElementAt(0);
				         try {
				            if (con.isClosed())
				               con = getConnection();
				         }
				         catch (Exception ex) {
				            ex.printStackTrace();
				         }
						}
						else if (MaxConn == 0 || ConNow 							con = new Connection();
						}
						if (con != null) {
							ConNow++;
						}
						return con;
					}
				
				
					private Connection newConnection() {
						Connection con = null;
						try {
							Class.forName(DriverName);
							con = DriverManager.getConnection(DbId,UserName, Password);
						}
						catch (Exception e) {
							e.printStackTrace();
							return null;
						}
						return con;
					}
				
					public synchronized void closeConn() {
						Enumeration allConnections = connections.elements();
						while (allConnections.hasMoreElements()) {
							Connection con = (Connection) allConnections.nextElement();
							try {
								con.close();
							}
							catch (SQLException e) {
								e.printStackTrace();
							}
						}
						connections.removeAllElements();
					}
				}
							

相关资源