【问题标题】:Is it necessary to close PreparedStatement before preparing another Statement在准备另一个Statement之前是否需要关闭PreparedStatement
【发布时间】:2015-10-02 07:21:55
【问题描述】:

是否需要在一个 db.getConnection() 中关闭 ResultSet 和 PreparedStatement?下面的例子:

Connection conn = db.getConnection();
PreparedStatement pstmt = conn.prepareStatement(firstsql);
ResultSet r = pstmt.executeQuery();
// do something with the ResultSet
r.close();
pstmt.close();   // do I need close the r and pstmt here?
PreparedStatement pstmt = conn.prepareStatement(secondsql);
ResultSet r = pstmt.executeQuery();
// do something with the ResultSet again
r.close();
pstmt.close();
conn.close();
return null;

第5行和第6行的代码有必要吗?

【问题讨论】:

    标签: java database jdbc prepared-statement


    【解决方案1】:

    严格来说,没有必要,因为您可以同时打开多个准备好的语句。 必要的是关闭每个打开的资源,如果它以后不会被使用。

    查看您的代码,它并不能确保该语句实际上是关闭的。

    为确保这一点,每个关闭操作都必须在 finally 块内完成:这样,无论操作成功与否,它都会执行。

    示例代码:

    PreparedStatement pstmt = null;
    ResultSet r = null;
    try {
        pstmt = conn.prepareStatement(firstsql);
        r = pstmt.executeQuery();
        // do something with the ResultSet
    } finally {
        if (r != null) {
            try {
                r.close();
            } catch (SQLException e) {
                //log error
            }
        }
        if (pstmt != null) {
            try {
                pstmt.close();
            } catch (SQLException e) {
                //log error
            }
        }
    }
    //and do it again
    

    如果您使用带有try-with-resources 语句的Java 7,则可以大大简化此代码:

    try (PreparedStatement pstmt = conn.prepareStatement(firstsql);
          ResultSet r = pstmt.executeQuery();) {
        // do something with the ResultSet
    }
    

    【讨论】:

      【解决方案2】:

      没有。您可以同时打开多个语句。但你最终必须关闭它们。

      您发布的代码无法编译,如果编译了,则会发生资源泄漏,但这与您标题中的问题不同。

      【讨论】:

      • 谢谢。阅读完所有 cmets 后,我明白您所说的“否”和“资源泄漏”是什么意思。
      猜你喜欢
      • 1970-01-01
      • 2013-01-10
      • 2014-11-10
      • 2023-03-29
      • 2015-11-04
      • 2010-09-23
      • 2021-07-10
      • 2014-08-04
      • 1970-01-01
      相关资源
      最近更新 更多