package com.casic.birmm.util; import java.sql.Connection; import java.sql.SQLException; import com.mchange.v2.c3p0.ComboPooledDataSource; public class ConnectionManager { public static ComboPooledDataSource dsOracle; public static ComboPooledDataSource dsSQLServer; public static ComboPooledDataSource dsMySQL; public static ComboPooledDataSource dsMySQLDest; static { // 连接Oracle数据库 try { dsOracle = new ComboPooledDataSource(); String host = Configure.getProperty("DB.ORACLE.HOST", "localhost"); String port = Configure.getProperty("DB.ORACLE.PORT", "1521"); String dbname = Configure.getProperty("DB.ORACLE.SERVICE", "dxcg"); String url = "jdbc:oracle:thin:@" + host + ":" + port + ":" + dbname; System.out.println(url); dsOracle.setJdbcUrl(url); dsOracle.setUser(Configure.getProperty("DB.ORACLE.USERNAME", "dxcg")); dsOracle.setPassword(Configure.getProperty("DB.ORACLE.PASSWORD")); dsOracle.setDriverClass("oracle.jdbc.driver.OracleDriver"); } catch (Exception e) { dsOracle = null; e.printStackTrace(); } // 连接SQL Server数据库 try { dsSQLServer = new ComboPooledDataSource(); String host = Configure.getProperty("DB.SQLSERVER.HOST", "localhost"); String port = Configure.getProperty("DB.SQLSERVER.PORT", "1433"); String dbname = Configure.getProperty("DB.SQLSERVER.SERVICE", "IRIS_DEVICE"); String url = "jdbc:sqlserver://" + host + ":" + port + ";DatabaseName=" + dbname; System.out.println(url); dsSQLServer.setJdbcUrl(url); String username = Configure.getProperty("DB.SQLSERVER.USERNAME", "sa"); String password = Configure.getProperty("DB.SQLSERVER.PASSWORD", "sa"); dsSQLServer.setUser(username); dsSQLServer.setPassword(password); dsSQLServer.setDriverClass("com.microsoft.sqlserver.jdbc.SQLServerDriver"); } catch (Exception e) { dsSQLServer = null; e.printStackTrace(); } // 连接MySQL数据库 try { dsMySQL = new ComboPooledDataSource(); String host = Configure.getProperty("DB.MYSQL.HOST", "localhost"); String port = Configure.getProperty("DB.MYSQL.PORT", "3306"); String dbname = Configure.getProperty("DB.MYSQL.SERVICE", "casic_iris_birmm"); String url = "jdbc:mysql://" + host + ":" + port + "/" + dbname + "?characterEncoding=utf8"; System.out.println(url); dsMySQL.setJdbcUrl(url); String username = Configure.getProperty("DB.MYSQL.USERNAME", "root"); String password = Configure.getProperty("DB.MYSQL.PASSWORD", "root"); dsMySQL.setUser(username); dsMySQL.setPassword(password); dsMySQL.setDriverClass("com.mysql.jdbc.Driver"); } catch (Exception e) { dsMySQL = null; e.printStackTrace(); } // 连接MySQL目标数据库 try { dsMySQLDest = new ComboPooledDataSource(); String host = Configure.getProperty("DB.MYSQL.DEST.HOST", "localhost"); String port = Configure.getProperty("DB.MYSQL.DEST.PORT", "3306"); String dbname = Configure.getProperty("DB.MYSQL.DEST.SERVICE", "casic_iris_birmm"); String url = "jdbc:mysql://" + host + ":" + port + "/" + dbname + "?characterEncoding=utf8"; System.out.println(url); dsMySQLDest.setJdbcUrl(url); String username = Configure.getProperty("DB.MYSQL.DEST.USERNAME", "root"); String password = Configure.getProperty("DB.MYSQL.DEST.PASSWORD", "root"); dsMySQLDest.setUser(username); dsMySQLDest.setPassword(password); dsMySQLDest.setDriverClass("com.mysql.jdbc.Driver"); } catch (Exception e) { dsMySQLDest = null; e.printStackTrace(); } } /** * 从C3P0的连接池中获取Oracle数据库的jdbc连接 * @return */ public static Connection getOracleConnectionFromC3P0() { Connection conn = null; if (null != dsOracle) { try { conn = dsOracle.getConnection(); } catch (SQLException e) { e.printStackTrace(); } } return conn; } /** * 从C3P0的连接池中获取SQLServer数据库的jdbc连接 * @return */ public static Connection getSQLServerConnectionFromC3P0() { Connection conn = null; if (null != dsSQLServer) { try { conn = dsSQLServer.getConnection(); } catch (SQLException e) { e.printStackTrace(); } } return conn; } /** * 从C3P0的连接池中获取MySQL数据库的jdbc连接 * @return */ public static Connection getMySQLConnectionFromC3P0() { Connection conn = null; if (null != dsMySQL) { try { conn = dsMySQL.getConnection(); } catch (SQLException e) { e.printStackTrace(); } } return conn; } /** * 从C3P0的连接池中获取MySQL目标数据库的jdbc连接 * @return */ public static Connection getMySQLDestConnectionFromC3P0() { Connection conn = null; if (null != dsMySQLDest) { try { conn = dsMySQLDest.getConnection(); } catch (SQLException e) { e.printStackTrace(); } } return conn; } public static void main(String[] args) { Connection conn1 = ConnectionManager.getOracleConnectionFromC3P0(); Connection conn2 = ConnectionManager.getSQLServerConnectionFromC3P0(); Connection conn3 = ConnectionManager.getMySQLConnectionFromC3P0(); System.out.println(conn1); System.out.println(conn2); System.out.println(conn3); } }