mysql 工具类:包含mysql数据库的连接和关闭方法

mysql 工具类:包含mysql数据库的连接和关闭方法,第1张

mysql 工具类:包含mysql数据库的连接关闭方法
package Utils;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;

public class Utils {
    private static String user;
    private static String pwd;
    private static String driver;
    private static String url;


    static {
        try {
            Properties properties = new Properties();
            properties.load(new FileInputStream("src\mysql.properties"));
            user = properties.getProperty("user");
            pwd = properties.getProperty("password");
            driver = properties.getProperty("driver");
            url = properties.getProperty("url");
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
        
    public static Connection connection(){
        try {
            Class.forName(driver);
            return DriverManager.getConnection(url,user,pwd);
        } catch (ClassNotFoundException | SQLException e) {
            throw new RuntimeException(e);
        }
    }
    public static void close(ResultSet set,Statement statement,Connection connection){
        try {
            if(set!=null){
                set.close();
            }
            if (statement!=null){
                statement.close();
            }
            if(connection!=null){
                connection.close();
            }
        } catch (SQLException e) {
            throw  new RuntimeException(e);
        }
    }

}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5673096.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存