代码之家  ›  专栏  ›  技术社区  ›  TheItalianJobless

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

  •  0
  • TheItalianJobless  · 技术社区  · 7 年前

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

    设计中是否有不只是主观的约定?

    1 回复  |  直到 7 年前
        1
  •  1
  •   ujulu    7 年前

    假设您使用的是Java 1.7及以上版本,您可以使用 try with resources 简化资源关闭的声明。前提是资源实现 AutoClosable Connection PreparedStatement

    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;
    }