package com.lantone.qc.trans.util.http.db;

/**
 * @description:
 * @author: zhoutg
 * @time: 2020/4/5 10:56
 */

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

public class DBUtil {

    //将获得的数据库与java的链接返回（返回的类型为Connection）
    public static Connection getConnection(){
        Connection connection = null;
        try {
            //1.加载驱动程序
            Class.forName("com.mysql.jdbc.Driver");
            //2.获得数据库的连接
            connection = DriverManager.getConnection("jdbc:mysql://192.168.2.236:3306/sys-qctest?useUnicode=true&characterEncoding=UTF-8", "root", "lantone");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        };
        return connection;
    }

    //将获得的数据库与java的链接返回（返回的类型为Connection）
    public static Statement getStatement(){
        Connection connection = getConnection();
        Statement st = null;
        try {
            //1.加载驱动程序
            st = connection.createStatement();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return st;
    }

    // 释放资源
    public static void release(Connection connection, Statement st, ResultSet rs ){
        try {
            connection.close();
            st.close();
            rs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
