java - 这两种方法中哪一种是处理使用 JDBC 的 DAO 类中关闭 ResultSet、PreparedStatement 和 Connection 的 try-catch 的最佳方法?

标签 java jdbc exception dao

我正在创建一个使用 JDBC 和 MySQL 的 DAO 类。我没有收到任何关于如何关闭标题中列出的项目的指示,但我读到这样做是一个很好的做法。现在我认为这应该在每个 CRUD 方法中完成,但处理异常似乎有点人为,我还不确定如何实现它。

第一个例子:

public boolean update2(Dto dto) {
    assert dto != null;
    if (readById(dto.getId()).getId() == 0) {
        throw new RuntimeException("Row with this id doesn't exist");
    }
    boolean flag = false;
    try {
        Connection connection = DAOFactory.createConnection();
        String sql = "SQL statement"; 
        try {
            PreparedStatement ps = connection.prepareStatement(sql);
            try {
                // Some stuff with preparedstatement
                ps.executeUpdate();
                flag = true;
            } finally {
                if (ps != null) ps.close();
            }
        } finally {
            if (connection != null) connection.close();
        }
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return flag;
}

第二个例子:

public boolean update(Dto dto) {
    assert dto != null;
    if (readById(dto.getId()).getId() == 0) {
        throw new RuntimeException("Row with this id doesn't exist");
    }
    boolean flag = false;
    PreparedStatement ps = null;
    Connection connection = null;
    try {
        connection = DAOFactory.createConnection();
        String sql = "SQL statement"; 
        ps = connection.prepareStatement(sql);
        // Some stuff with preparedstatement
        ps.executeUpdate();
        flag = true;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    return flag;
}

在第二个示例中,我需要重复的异常处理。第一个解决方案对我来说似乎更聪明,但我不确定它比第二个解决方案更具可读性。

是否有一些设计惯例不仅是主观的?

最佳答案

假设您使用的是Java 1.7及以上版本,您可以使用try with resources语句来简化资源的关闭。如果资源实现了 AutoClosable 接口(interface)(ConnectionPreparedStatement 就是这种情况),您可以按如下方式重写代码:

public boolean update2(String dto) {
    assert dto != null;

    if (readById(dto.getId()).getId() == 0) {
        throw new RuntimeException("Row with this id doesn't exist");
    }

    boolean flag = false;
    String sql = "SQL statement";
    try (Connection connection = DAOFactory.createConnection();
         PreparedStatement ps = connection.prepareStatement(sql)) {
        ps.executeUpdate();
        flag = true;
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return flag;
}

关于java - 这两种方法中哪一种是处理使用 JDBC 的 DAO 类中关闭 ResultSet、PreparedStatement 和 Connection 的 try-catch 的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46326422/

相关文章:

java - 单击国际化按钮后如何重新加载Vaadin中的组件?

java - 使用 Hibernate 从 Oracle DB 获取 500K+ 行

java - 使用来自 Eclipse 的 JDBC 的 DB2 连接问题

python - QThread异常管理和线程竞争

exception - 如何捕获人脸 FileNotFoundException?

java - 可以找到元素 'context:property-placeholder' 的声明。 Spring 4

java - 网站按钮上的 "Click"

spring - InvalidDataAccessResourceUsageException : Unexpected cursor position change

java - 在sql server 2008中调用带有表类型输入参数的存储过程

java - Kafka 宕机时如何处理 IOException?