日期:2014-05-16  浏览次数:20451 次

Java数据库连接池实现

首先写一个读取本地数据库驱动,数据库用户名、数据库密码、连接数的类。

?

?

package cn.cate.utils;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;

public class PoolProperties {

	private Properties properties;
	private String driver;
	private String url;
	private String user;
	private String password;
	private int maxConn;
	private int minConn;
	
	public PoolProperties(){
		properties = new Properties();
		String path = getClass().getProtectionDomain().getCodeSource().getLocation().toString();
		path = path.substring(6, path.length());
		if (path.indexOf("WEB-INF") > 0) {
		      path = path.substring(0, path.indexOf("/WEB-INF") + 9) + "lib/";
		}else{
		      path = "d:";
		}
		try {
			this.properties.load(new FileReader(path + "db.pro"));
		}catch (FileNotFoundException e) {
		    e.printStackTrace();
		}catch (IOException e) {
		    e.printStackTrace();
		}
		this.setDriver(properties.getProperty("driver"));
		this.setUrl(properties.getProperty("url"));
		this.setUser(properties.getProperty("user"));
		this.setPassword(properties.getProperty("password"));
		this.setMaxConn(Integer.parseInt(properties.getProperty("maxConn")));
		this.setMinConn(Integer.parseInt(properties.getProperty("minConn")));
	}
	
	
	public Properties getProperties() {
		return properties;
	}

	public void setProperties(Properties properties) {
		this.properties = properties;
	}

	public String getDriver() {
		return driver;
	}

	public void setDriver(String driver) {
		this.driver = driver;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public int getMaxConn() {
		return maxConn;
	}

	public void setMaxConn(int maxConn) {
		this.maxConn = maxConn;
	}

	public int getMinConn() {
		return minConn;
	}

	public void setMinConn(int minConn) {
		this.minConn = minConn;
	}
	
	
}

?

?

db.pro的内容如下:

?

driver=com.mysql.jdbc.Driver ?? ? ? ?maxConn=10??? ?? ? ? ?minConn=1?? ?? ? ? ?url=jdbc:mysql://127.0.0.1:3306/cate?? ?? ? ? ?user=root?? ?? ? ? ?password=11111??

?

然后定义一个数据库连接池:DBConnectionPool

?

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/*
 * 此内部类定义了一个连接池,它能够根据要求创建连接,直到预定的最大连接数为止。
 * 在返回连接给客户程序之前,它能够验证连接的有效性。
 */
public class DBConnectionPool {

	/*
	 * 连接池
	 */
	private List<Connection> freeConnections = new ArrayList<Connection>();
	private Connection conn = null;
	private int connect = 0;     //使用的连接数
	private int maxConn;      //最大连接数
	private String driver;    //数据库驱动
	private String url;     //数据库连接地址
	private String user;     //用户名
	private String password;      //用户名
	
	public DBConnectionPool(String driver,String URL,String user,String password,int maxConn){
		this.driver = driver;
		this.url = URL;
		this.user = user;
		this.password = password;
		this.maxConn = maxConn;
		poolInfo();
	}
	
	/*
	 * 显示准备创建连接池的信息
	 */
	private void poolInfo(){
		Connection conn = this.newConnection();
		freeConnections.add(conn);
		for(int i = 0;i < this.maxConn - 1;i++){
			Connection freeConn = conn;
			freeConnections.add(freeConn);
		}
	}
	
	/*
	 * 用完,释放