我应该在 try-with-resources 语句中声明每个资源吗?
Should I declare EVERY resource in try-with-resources Statement?
在我搜索过的很多try-with-resource例子中,Statement和ResultSet是分开声明的。正如 Java 文档中提到的,资源的关闭方法的调用顺序与其创建顺序相反。
try (Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql) ) {
} catch (Exception e) {
}
但现在我的函数中有多个查询。
我可以在一行中创建Statement 和ResultSet 吗?我的代码是这样的:
try (ResultSet rs = con.createStatement().executeQuery(sql);
ResultSet rs2 = con.createStatement().executeQuery(sql2);
ResultSet rs3 = con.createStatement().executeQuery(sql3)) {
} catch (Exception e) {
}
如果我只在一行中声明它们,是否还会关闭 ResultSet 和 Statement 的资源?
如果你仔细看,你会发现这个概念叫做 try-with-resources.
注意复数!整个想法是您可以在该单个语句中声明一个或多个资源,并且 jvm 保证正确处理。
换句话说:当资源在语义上属于一起时,最好将它们一起声明。
是的,它的工作原理与您在问题中提出的完全一样,多个语句以分号分隔。
You may declare one or more resources in a try-with-resources statement. The following example retrieves the names of the files packaged in the zip file zipFileName and creates a text file that contains the names of these files:
try (
java.util.zip.ZipFile zf =
new java.util.zip.ZipFile(zipFileName);
java.io.BufferedWriter writer =
java.nio.file.Files.newBufferedWriter(outputFilePath, charset)
) {
// Enumerate each entry
for (java.util.Enumeration entries =
zf.entries(); entries.hasMoreElements();) {
// Get the entry name and write it to the output file
String newLine = System.getProperty("line.separator");
String zipEntryName =
((java.util.zip.ZipEntry)entries.nextElement()).getName() +
newLine;
writer.write(zipEntryName, 0, zipEntryName.length());
}
}
https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
ResultSet
实现了 AutoCloseable
,这意味着 try-with-resources 也会在使用完毕后强制关闭它。
https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html
在我搜索过的很多try-with-resource例子中,Statement和ResultSet是分开声明的。正如 Java 文档中提到的,资源的关闭方法的调用顺序与其创建顺序相反。
try (Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(sql) ) {
} catch (Exception e) {
}
但现在我的函数中有多个查询。
我可以在一行中创建Statement 和ResultSet 吗?我的代码是这样的:
try (ResultSet rs = con.createStatement().executeQuery(sql);
ResultSet rs2 = con.createStatement().executeQuery(sql2);
ResultSet rs3 = con.createStatement().executeQuery(sql3)) {
} catch (Exception e) {
}
如果我只在一行中声明它们,是否还会关闭 ResultSet 和 Statement 的资源?
如果你仔细看,你会发现这个概念叫做 try-with-resources.
注意复数!整个想法是您可以在该单个语句中声明一个或多个资源,并且 jvm 保证正确处理。
换句话说:当资源在语义上属于一起时,最好将它们一起声明。
是的,它的工作原理与您在问题中提出的完全一样,多个语句以分号分隔。
You may declare one or more resources in a try-with-resources statement. The following example retrieves the names of the files packaged in the zip file zipFileName and creates a text file that contains the names of these files:
try (
java.util.zip.ZipFile zf =
new java.util.zip.ZipFile(zipFileName);
java.io.BufferedWriter writer =
java.nio.file.Files.newBufferedWriter(outputFilePath, charset)
) {
// Enumerate each entry
for (java.util.Enumeration entries =
zf.entries(); entries.hasMoreElements();) {
// Get the entry name and write it to the output file
String newLine = System.getProperty("line.separator");
String zipEntryName =
((java.util.zip.ZipEntry)entries.nextElement()).getName() +
newLine;
writer.write(zipEntryName, 0, zipEntryName.length());
}
}
https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html
ResultSet
实现了 AutoCloseable
,这意味着 try-with-resources 也会在使用完毕后强制关闭它。
https://docs.oracle.com/javase/7/docs/api/java/sql/ResultSet.html