jsp超市管理系统系统,是用经典的MVC设计模式开发的非常适合初学者学习。

源代码在线查看: connpool.java

软件大小: 2045 K
上传用户: fansino
关键词: jsp MVC 超市管理系统
下载地址: 免注册下载 普通下载 VIP

相关代码

				package com.dbconn;
				
				import java.sql.*;
				import java.util.*;
				
				public class ConnPool {
				
					private int connNow = 0;						//现在连接数
					private int maxConn;							//最大连接数	
					private String poolName = null;				//连接池名字
					private String driverName = null;				//驱动程序名
					private String url = null;						//连接数据库url
					private String userName = null;				//数据库用户名
					private String pwd = null;						//数据库密码
					private Vector connections = new Vector();		//存放连接的容器
					
					public ConnPool(String poolName,String driverName,String url,String userName,String pwd,int maxConn){
						
						this.poolName = poolName;
						this.driverName = driverName;
						this.url = url;
						this.userName = userName;
						this.pwd = pwd;
						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 e){
								e.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(url,userName,pwd);
						}
						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(Exception e){
								e.printStackTrace();
							}
						}		
						connections.removeAllElements();		
					}
					
				}
				
							

相关资源