package util;

import java.sql.*;

public class SQLDatabaseConnection {
    // Connect to database
    private static final String connectionUrl =
            "jdbc:sqlserver://152.136.182.168:1433;"
                    + "database=Elm;"
                    + "user=guest;"
                    + "password=20230504#Guest;"
                    + "encrypt=true;"
                    + "trustServerCertificate=true;"
                    + "loginTimeout=30;";

    public static Connection getConnection() {
        Connection connection = null;
        try {
            // Code here.
            connection = DriverManager.getConnection(connectionUrl);
        }
        // Handle any errors that may have occurred.
        catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }

    //释放资源
    public static void close(Connection conn, Statement st, ResultSet rs) {
        try {
            if (rs != null) {
                rs.close();
            }
            if (st != null) {
                st.close();
            }
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
