package com.ozs.da.utils;

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

import com.ozs.da.entity.remotedb.MdmInfoDatabase;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

/**
 * @author 许明
 * on 2019/3/11.
 * @version 2.0.0
 */
public class ConnectionUtil {

    private static final Logger logger = LogManager.getLogger(ConnectionUtil.class);

    public static Connection initConn(MdmInfoDatabase mdmInfoDatabase) {
        //声明Connection对象
        Connection conn = null;

        //遍历查询结果集
        try {
        	//驱动程序名
            String driver = mdmInfoDatabase.getDriverName();
            //URL指向要访问的数据库名
            String url = mdmInfoDatabase.getUrl();
            //MySQL配置时的用户名
            String user = mdmInfoDatabase.getUsername();
            //MySQL配置时的密码
            String password = mdmInfoDatabase.getPassword();
            //加载驱动程序
            Class.forName(driver);
            //1.getConnection()方法，连接MySQL数据库！！
            conn = DriverManager.getConnection(url, user, password);
            if (!conn.isClosed()) {
                System.out.println("Succeeded connecting to the Database!");
            }
        } catch (ClassNotFoundException e) {
            //数据库驱动类异常处理
            System.out.println("Sorry,can`t find the Driver!");
            e.printStackTrace();
        } catch (SQLException e) {
            //数据库连接失败异常处理
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            System.out.println("数据库数据成功获取！！");
        }
        return conn;
    }
}
