繁体   English   中英

这两种方法中哪一种是处理尝试捕获以关闭使用JDBC的DAO类中的ResultSet,PreparedStatement和Connection的最佳方法?

[英]Which of these two is the best way to handle try-catches for closing ResultSet, PreparedStatement, and Connection in a DAO class that uses JDBC?

我正在创建一个使用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接口(在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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM