package cn.com.taiji.mqcrj.utils;
import lombok.extern.slf4j.Slf4j;

import java.sql.*;

/**
 * @author chen mh
 * @date 2023/7/25 17:59
 */
@Slf4j
public class JDBCUtils {


    private static final String user = "TO_SG";
    private static final String password = "TO_SG@6677";
    private static final String url ="jdbc:oracle:thin:@//74.6.50.235:1521/ORCL";
    private static final String driverClass ="oracle.jdbc.driver.OracleDriver";

    public static Connection getConnection(){
        Connection con = null;
        try {
            //获取参数
            log.info("数据库参数:"+user +password +url +driverClass);
            //2.加载驱动
            Class.forName(driverClass);
            //3.获取连接
            con = DriverManager.getConnection(url,user,password);
        } catch (Exception e) {
            log.info("数据库连接错误"+e);
            throw new RuntimeException(e);
        }
        return con;
    }

    /**
     * 关闭连接、Statement和ResultSet
     * @param con
     * @param ps
     */
    public static void closeResource(Connection con, Statement ps, ResultSet rs) {
        //资源关闭
        try {
            if(ps != null){
                ps.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(con != null){
                con.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(rs != null){
                rs.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }


}
