DBUtil.java 4.2 KB
package com.espeed.reading.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import javax.sql.DataSource;

import org.apache.commons.dbcp2.BasicDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 
 * 
 * @项目名称: 邮件跟踪系统
 * @版权所有: 深圳市科飞时速网络技术有限公司(0755-88843776)
 * @技术支持: info@21gmail.com
 * @单元名称: 获取数据库连接的工具类
 * @开始时间: 2018-5-23
 * @开发人员: 杨志钊
 */
public class DBUtil {

	private static final Log log = LogFactory.getLog(DBUtil.class);

	private static DataSource dataSource;
	
	static {
		// 加载配置文件
		Properties props = new Properties();

		InputStream is = null;
		try {

			is = new FileInputStream(DBUtil.class.getClassLoader()
					.getResource("").getPath()
					.replaceFirst("classes", "config.properties"));

			props.load(is);
			dataSource = BasicDataSourceFactory.createDataSource(props);

			printDataSourceStatus();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (is != null) {
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}

	}

	private DBUtil() {
	}

	/** 获取数据库连接对象 */
	public static Connection getConnection() throws SQLException {
		Connection conn = null;
		if (dataSource != null) {
			conn = dataSource.getConnection();
		}
		return conn;
	}

	/** 释放资源 */
	public static void close(ResultSet rs, Statement st, Connection conn) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (st != null) {
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public static String printDataSourceStatus() {
		BasicDataSource basicDataSource = (BasicDataSource) dataSource;

		StringBuilder builder = new StringBuilder();
		builder.append("数据库连接池启动成功,配置参数如下:\n");
		builder.append("初始化连接数:" + basicDataSource.getInitialSize() + "\n");
		builder.append("最大活动连接数:" + basicDataSource.getMaxTotal() + "\n");
		builder.append("最大空闲连接数:" + basicDataSource.getMaxIdle() + "\n");
		builder.append("最小空闲连接数:" + basicDataSource.getMinIdle() + "\n");
		builder.append("最大等待时间:" + (basicDataSource.getMaxWaitMillis() / 1000)
				+ "s\n");
		builder.append("超时时间:" + basicDataSource.getRemoveAbandonedTimeout()
				+ "s\n");
		builder.append("默认自动提交状态:" + basicDataSource.getDefaultAutoCommit()
				+ "\n");
		builder.append("\n@当前活跃连接数:" + basicDataSource.getNumActive() + "\n");
		builder.append("@当前空闲数:" + basicDataSource.getNumIdle() + "\n");

		log.warn(builder);
		
		return builder.toString();
	}

	// public static String DBUrl=ConfigPath.getBaseUrl();
	// public static String DBUser=ConfigPath.getBaseUserName();
	// public static String DBPassword=ConfigPath.getBasePassword();
	// public static final String DBDriver="com.mysql.jdbc.Driver";
	// public static Connection conn;
	// //加载驱动
	// static
	// {
	// try {
	// Class.forName(DBDriver);
	//
	// } catch (Exception e)
	// {
	// e.printStackTrace();
	// }
	// }
	// //获得连接
	// public static Connection getConnection() throws SQLException{
	// return DriverManager.getConnection(DBUrl, DBUser, DBPassword);
	// }
	//
	// /** 关闭数据库连接*/
	// public static void close(ResultSet rs,Statement st,Connection conn){
	// if(rs!=null){ try {
	// rs.close();
	// } catch (SQLException e) {
	// e.printStackTrace();
	// }finally{
	// if(st!=null){
	// try {
	// st.close();
	// } catch (SQLException e) {
	// e.printStackTrace();
	// }finally{
	// if(conn!=null){
	// try {
	// conn.close();
	// } catch (SQLException e) {
	// e.printStackTrace();
	// }
	// }
	// }
	// }
	// }
	// }
	// }
}