首页 新闻 赞助 找找看

数据库连接池就是一个简单连接工具?

0
悬赏园豆:5 [待解决问题]

就是一个包括自动连接和关闭的工具类么?package com.chunkyo.db;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Timer;
/**
 * @author chenyanlin
 *
 */
public class DBConnectionPool implements TimerListener {
 private Connection con=null;
 private int inUsed=0;    //使用的连接数
 private ArrayList freeConnections = new ArrayList();//容器,空闲连接
 private int minConn;     //最小连接数
 private int maxConn;     //最大连接
 private String name;     //连接池名字
 private String password; //密码
 private String url;      //数据库连接地址
 private String driver;   //驱动
 private String user;     //用户名
 public Timer timer;      //定时
 /**
  *
  */
 public DBConnectionPool() {
  // TODO Auto-generated constructor stub
 }
 /**
  * 创建连接池
  * @param driver
  * @param name
  * @param URL
  * @param user
  * @param password
  * @param maxConn
  */
 public DBConnectionPool(String name, String driver,String URL, String user, String password, int maxConn)
 {
  this.name=name;
  this.driver=driver;
  this.url=URL;
  this.user=user;
  this.password=password;
  this.maxConn=maxConn;
 }
 /**
  * 用完,释放连接
  * @param con
  */
 public synchronized void freeConnection(Connection con)
 {
  this.freeConnections.add(con);//添加到空闲连接的末尾
  this.inUsed--;
 }
 /**
  * timeout  根据timeout得到连接
  * @param timeout
  * @return
  */
 public synchronized Connection getConnection(long timeout)
 {
  Connection con=null;
  if(this.freeConnections.size()>0)
  {
   con=(Connection)this.freeConnections.get(0);
   if(con==null)con=getConnection(timeout); //继续获得连接
  }
  else
  {
   con=newConnection(); //新建连接
  }
  if(this.maxConn==0||this.maxConn<this.inUsed)
  {
   con=null;//达到最大连接数,暂时不能获得连接了。
  }
  if(con!=null)
  {
   this.inUsed++;
  }
  return con;
 }
 /**
  *
  * 从连接池里得到连接
  * @return
  */
 public synchronized Connection getConnection()
 {
  Connection con=null;
  if(this.freeConnections.size()>0)
  {
   con=(Connection)this.freeConnections.get(0);
   this.freeConnections.remove(0);//如果连接分配出去了,就从空闲连接里删除
   if(con==null)con=getConnection(); //继续获得连接
  }
  else
  {
   con=newConnection(); //新建连接
  }
  if(this.maxConn==0||this.maxConn<this.inUsed)
  {
   con=null;//等待 超过最大连接时
  }
  if(con!=null)
  {
   this.inUsed++;
   System.out.println("得到 "+this.name+" 的连接,现有"+inUsed+"个连接在使用!");
  }
  return con;
 }
 /**
  *释放全部连接
  *
  */
 public synchronized void release()
 {
  Iterator allConns=this.freeConnections.iterator();
  while(allConns.hasNext())
  {
   Connection con=(Connection)allConns.next();
   try
   {
    con.close();
   }
   catch(SQLException e)
   {
    e.printStackTrace();
   }
   
  }
  this.freeConnections.clear();
   
 }
 /**
  * 创建新连接
  * @return
  */
 private Connection newConnection()
 {
  try {
   Class.forName(driver);
   con=DriverManager.getConnection(url, user, password);
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
   System.out.println("sorry can't find db driver!");
  } catch (SQLException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
   System.out.println("sorry can't create Connection!");
  }
  return con;
  
 }
 /**
  * 定时处理函数
  */
 public synchronized void TimerEvent()
 {
     //暂时还没有实现以后会加上的
 }
 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
 }
 /**
  * @return the driver
  */
 public String getDriver() {
  return driver;
 }
 /**
  * @param driver the driver to set
  */
 public void setDriver(String driver) {
  this.driver = driver;
 }
 /**
  * @return the maxConn
  */
 public int getMaxConn() {
  return maxConn;
 }
 /**
  * @param maxConn the maxConn to set
  */
 public void setMaxConn(int maxConn) {
  this.maxConn = maxConn;
 }
 /**
  * @return the minConn
  */
 public int getMinConn() {
  return minConn;
 }
 /**
  * @param minConn the minConn to set
  */
 public void setMinConn(int minConn) {
  this.minConn = minConn;
 }
 /**
  * @return the name
  */
 public String getName() {
  return name;
 }
 /**
  * @param name the name to set
  */
 public void setName(String name) {
  this.name = name;
 }
 /**
  * @return the password
  */
 public String getPassword() {
  return password;
 }
 /**
  * @param password the password to set
  */
 public void setPassword(String password) {
  this.password = password;
 }
 /**
  * @return the url
  */
 public String getUrl() {
  return url;
 }
 /**
  * @param url the url to set
  */
 public void setUrl(String url) {
  this.url = url;
 }
 /**
  * @return the user
  */
 public String getUser() {
  return user;
 }
 /**
  * @param user the user to set
  */
 public void setUser(String user) {
  this.user = user;
 }

}

中华博客园的主页 中华博客园 | 菜鸟二级 | 园豆:201
提问于:2014-10-12 13:21
< >
分享
所有回答(1)
0

你Google一下数据库连接池,应该会有一堆达人跟你解释这个啥意思的啊。

你要是不懂的话,多半是因为你没有需要,等你需要的时候再懂也不迟。

爱编程的大叔 | 园豆:30839 (高人七级) | 2014-10-13 07:30

谢谢

支持(0) 反对(0) 中华博客园 | 园豆:201 (菜鸟二级) | 2014-10-13 08:30
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册