package com.szpg.db.util;

import java.sql.Connection;
import java.sql.SQLException;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.szpg.util.Configure;

public class ConnectionManager {
	public static ComboPooledDataSource dataSource;
	
	static {
		try {
			dataSource = new ComboPooledDataSource();
			
			String host = Configure.getProperty("sys", "DB.HOST", "localhost");
			String port = Configure.getProperty("sys", "DB.PORT", "1521");
			String dbname = Configure.getProperty("sys", "DB.SERVICE", "ORCL");
			String url = "jdbc:oracle:thin:@//" + host + ":" + port + "/" + dbname;
			dataSource.setJdbcUrl(url);
			
			dataSource.setUser(Configure.getProperty("sys", "DB.USERNAME", "scott"));
			dataSource.setPassword(Configure.getProperty("sys", "DB.PASSWORD", "tiger"));
			
			dataSource.setDriverClass("oracle.jdbc.driver.OracleDriver");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 从C3P0的连接池中获取jdbc连接
	 * @return
	 */
	public static Connection getConnectionFromC3P0() {
		Connection conn = null;
		if (null != dataSource) {
			try {
				conn = dataSource.getConnection();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return conn;
	}
}