且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

java.sql.SQLException:数据库连接已关闭

更新时间: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;
}