用于JSP1.0 开发的一个类似于动网的BBS论坛。 数据库是DB2+JSP+javaBean

源代码在线查看: connpool.java

软件大小: 22715 K
上传用户: lujing200912345
关键词: JSP javaBean 1.0 BBS
下载地址: 免注册下载 普通下载 VIP

相关代码

				package chapter10;
				
				import java.io.Serializable;
				import java.sql.*;
				import java.util.*;
				
				public class ConnPool implements java.io.Serializable{
				  private String driver = null;
				  private String url = null;
				  private int size = 0;
				  private String username = "";
				  private String password = "";
				  private DbConn dc=null;
				  private Vector pool = null;
				
				  public ConnPool(){}
				
				  public void setDriver(String driver){
				    if (driver!=null) this.driver=driver;
				  }
				  public String getDriver(){
				    return driver;
				  }
				
				  public void setURL(String url){
				    if (url!=null) this.url=url;
				  }
				  public String getURL(){
				    return url;
				  }
				
				  public void setSize(int size){
				    if (size>1) this.size=size;
				  }
				  public int getSize(){
				    return size;
				  }
				
				  public void setUsername(String username){
				    if (username!=null) this.username=username;
				  }
				  public String getUserName(){
				    return username;
				  }
				
				  public void setPassword(String password){
				    if (password!=null) this.password=password;
				  }
				  public String getPassword(){
				    return password;
				  }
				//以上主要是设置其数据的连接参数
				  public void setConnBean(DbConn dc){
				    if (dc!=null) this.dc=dc;
				  }
				  public DbConn getConnBean() throws Exception{
				    Connection conn = getConnection();
				    DbConn dc = new DbConn(conn);
				    dc.setInuse(true);
				    return dc;
				  }
				
				  private Connection createConnection() throws Exception{
				    Connection con = null;
				    con = DriverManager.getConnection(url,username,password);
				    return con;
				  }
				
				  public synchronized void initializePool() throws Exception{
				    if (driver==null)
				      throw new Exception("No Driver Provided!");
				    if (url==null)
				      throw new Exception("No URL Proviced!");
				    if (size				      throw new Exception("Connection Pool Size is less then 1!");
				    try{
				      Class.forName(driver);
				      for (int i=0; i				        Connection con = createConnection();
				        if (con!=null){
				          DbConn dc = new DbConn(con);
				          addConnection(dc);
				        }
				      }
				    }catch (Exception e){
				      System.err.println(e.getMessage());
				      throw new Exception(e.getMessage());
				    }
				  }
				
				  private void addConnection(DbConn conn){
				    if (pool==null) pool=new Vector(size);
				    pool.addElement(conn);
				  }
				
				  public synchronized void releaseConnection(Connection con){
				    for (int i=0; i				      DbConn connBean = (DbConn)pool.elementAt(i);
				      if (connBean.getConnection()==con){
				        System.err.println("Release No." + i + " Connection!");
				        connBean.setInuse(false);
				        break;
				      }
				    }
				  }
				
				  public synchronized Connection getConnection() throws Exception{
				    DbConn dc = null;
				    for (int i=0; i				      dc = (DbConn)pool.elementAt(i);
				      if (dc.getInuse()==false){
				        dc.setInuse(true);
				        Connection con = dc.getConnection();
				        return con;
				      }
				    }
				    try{
				      Connection con = createConnection();
				      dc = new DbConn(con);
				      dc.setInuse(true);
				      pool.addElement(dc);
				    }catch (Exception e){
				      System.err.println(e.getMessage());
				      throw new Exception(e.getMessage());
				    }
				    return dc.getConnection();
				  }
				
				  public synchronized void emptyPool(){
				    for (int i=0; i				      System.err.println("Close No." + i + " JDBC Connection!");
				      DbConn connBean = (DbConn)pool.elementAt(i);
				      if (dc.getInuse()==false)
				        dc.close();
				      else{
				        try{
				          java.lang.Thread.sleep(20000);
				          dc.close();
				        }catch (InterruptedException ie){
				          System.err.println(ie.getMessage());
				        }
				      }
				    }
				   }
				}
				//连接数据库的javabean
				class DbConn implements java.io.Serializable{
				  private Connection conn = null;
				  private boolean inuse = false;
				
				  public DbConn(){}
				  public DbConn(Connection conn){
				    if (conn!=null) this.conn = conn;
				  }
				
				  public Connection getConnection(){
				    return conn;
				  }
				  public void setConnection(Connection conn){
				    this.conn = conn;
				  }
				
				  public void setInuse(boolean inuse){
				    this.inuse = inuse;
				  }
				  public boolean getInuse(){
				    return inuse;
				  }
				
				  public void close(){
				    try{
				      conn.close();
				    }catch (SQLException sqle){
				      System.err.println(sqle.getMessage());
				    }
				  }
				}
							

相关资源