jdbc工具类

public class JDBCUtils {
    private static Connection connection;
    static {
        try {
            Properties properties = new Properties();
            properties.load(new FileReader("src\\mysql.properties"));
            String url = properties.getProperty("url");
            String user = properties.getProperty("user");
            String pwd = properties.getProperty("pwd");
            connection = DriverManager.getConnection(url, user, pwd);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        return connection;
    }

    public static boolean Close(ResultSet resultSet, PreparedStatement statement,Connection connection){
        try {
            if(resultSet != null){
                resultSet.close();
            }
            if(statement!=null){
                statement.close();
            }
            if(connection!=null){
                connection.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }
}

注意:静态方法只能使用静态属性,但是普通方法既能使用静态属性,又能使用非静态属性。

posted @ 2023-01-30 21:52  zwGitOne  阅读(21)  评论(0编辑  收藏  举报