更新时间:2023-11-16 18:06:46
当离开范围时,此try-with-resources
构造将在Connection上调用方法close
:
This try-with-resources
construct will invoke method close
on Connection when you leave its scope:
try (Connection conn = DriverManager.getConnection(url)) {
return conn;
}
因此,您可以有效地返回关闭的连接.
So, you effectively return closed connection.
将其更改为常规的try
构造以返回实时连接:
Change it to regular try
construct to return live connection:
try {
Connection conn = DriverManager.getConnection(url);
return conn;
} catch (SQLException e) {
return null;
}