【发布时间】:2014-08-01 20:55:26
【问题描述】:
这个问题已经被问过很多次了,有很多资源都在讨论这个问题。但这仍然让我担心,因为我认为 close() 无法正常工作。
PreparedStatemet pstmt = null;
try {
pstmt = conn.prepareStatement(query);
...
pstmt.close();
conn.close();
} catch(...) {
...
} finally {
if(pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
pstmt = null;
}
}
if(conn != null) {
try {
conn.close();
} catch (SQLException e) {
conn = null;
}
}
System.out.println("PreparedStatement: " + pstmt);
System.out.println("Connection: " + conn);
}
所以我希望它会打印出 null;但它会不断打印出查询字符串和数据库的连接路径。
【问题讨论】:
-
为什么?它可以打印出任何它喜欢的东西。您的期望显然是错误的。
-
对不起,我不明白你的意思。我认为当我们关闭 ptsmt 和 conn 时;他们都应该为空,不是吗?
-
没有。 (a) 当您将它们设置为 null 时,引用变为 null。不然。 (b) 您看到的是在非空引用上调用 toString() 的结果,该引用可以是相关类喜欢的任何内容。
-
哦,我明白了。感谢您的澄清。
标签: jdbc connection