关于eclipse数据库连接池封装使用

版权声明:未经允许不得随意转载,著作权归__盛某人所有 https://blog.csdn.net/NaXieNianWoMenYiQ/article/details/87973198
  1. 创建一个文件,在src目录下,文件名为"db.properties"

    文件中以键值对形式记录,地址(url),数据库名(username),数据库密码(username)
    连接池最大数(MaxPoolSize),连接池最小数(MinPoolSize),初始化连接池连接数量(
    initialPoolSize),当连接池用完时客户端调用getConnection()后等待获取新连接的时间
    ,超时后将抛出 SQLException,如设为0则无限期等待。单位毫秒,默认为0
    

    代码具体如下:

    drivername="com.mysql.jdbc.driver"
    url="jdbc:mysql://localhost:3306/mvc"
    username="root"
    pwd="root"
    
    maxPoolSize=20
    minPoolSize=5
    initialPoolSize=10
    checkoutTimeout=10000
    
    
  2. 在工具包(com.java.util)下创建连接池类
    代码如下:

package com.java.util;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/**
 * 数据库连接池 c3p0数据源
 * 
 * @author Administrator
 */
public class MyDataSource {

	// 创建静态变量用于获取存放db.properties文件中的数据
	public static String DRIVER;
	public static String URL;
	public static String USER;
	public static String PWD;
	public static int maxPoolSize;
	public static int minPoolSize;
	public static int initialPoolSize;
	public static int checkoutTimeout;
	//保存文件名
	private static final String filePath = "db.properties";
	//连接池初始化
	private static ComboPooledDataSource cpDataSource = null;

	// 加载驱动
	/*static代码块 ,也叫静态代码块,
		是在类中独立于类成员的static语句块,可以有多个,位置可以随便放,
		它不在任何的方法体内,JVM加载类时会执行这些静态的代码块,
		如果static代码块有多个,JVM将按照它们在类中出现的先后顺序依次执行它们,
		每个代码块只会被执行一次
		利用静态代码块可以对一些static变量进行赋值*/
	static {
		try {
		
			// 读取配置文件,加载JDBC四大参数
			//Properties是Java中jdk自带的一个对象
			Properties config = new Properties();
			
			//我们可以直接将后缀为properties的文件变为Properties对象,
			//然后通过Porperties对象中的
			
			config.load(MyDataSource.class.getClassLoader().getResourceAsStream(filePath));
			
			//public synchronized Object setProperty(String key, String value)
			//public String getProperty(String key)
			//获取db.properties文件中的数据
			DRIVER = config.getProperty("drivername");
			URL = config.getProperty("url");
			USER = config.getProperty("username");
			PWD = config.getProperty("password");

			maxPoolSize = Integer.parseInt(config.getProperty("maxPoolSize"));
			minPoolSize = Integer.parseInt(config.getProperty("minPoolSize"));
			initialPoolSize = Integer.parseInt(config.getProperty("initialPoolSize"));
			checkoutTimeout = Integer.parseInt(config.getProperty("checkoutTimeout"));

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**************** c3p0 数据库连接池 启动方法 ******************/
	private static void c3p0DataSource() throws Exception {
		//创建连接池
		cpDataSource = new ComboPooledDataSource();
		//设置连接池参数
		cpDataSource.setDriverClass(DRIVER);
		cpDataSource.setJdbcUrl(URL);
		cpDataSource.setUser(USER);
		cpDataSource.setPassword(PWD);
		cpDataSource.setMaxPoolSize(maxPoolSize);
		cpDataSource.setMinPoolSize(minPoolSize);
		cpDataSource.setInitialPoolSize(initialPoolSize);
		cpDataSource.setCheckoutTimeout(checkoutTimeout);
	}

	/**
	 * c3p0数据库连接入
	 * 
	 * @return
	 * @throws Exception
	 */
	public static Connection getConnection() {
		Connection conn = null;
		
		try {
		//如果连接池不存在就调用连接池创建方法
			if (cpDataSource == null) {
				c3p0DataSource();
			} 
			如果连接不存在,通过连接池获得一个连接
			if(conn == null){
				conn = cpDataSource.getConnection();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//返回一个连接
		return conn;
	}

}

猜你喜欢

转载自blog.csdn.net/NaXieNianWoMenYiQ/article/details/87973198