auto close prepared statement

This commit is contained in:
mlus 2025-11-20 12:18:26 +08:00
parent 1b4cfe4e39
commit c2d5d37d30

View File

@ -49,7 +49,7 @@ public class JDBCsetUp {
Connection connection = getConnection(); // With database selected (and "USE" already run)
PreparedStatement queryStatement = connection.prepareStatement(sql);
ResultSet resultSet = queryStatement.executeQuery();
return new QueryResult(connection, resultSet);
return new QueryResult(connection, queryStatement, resultSet);
}
/**
@ -94,7 +94,7 @@ public class JDBCsetUp {
}
}
public record QueryResult(Connection connection, ResultSet resultSet) implements AutoCloseable {
public record QueryResult(Connection connection,PreparedStatement preparedStatement, ResultSet resultSet) implements AutoCloseable {
@Override
public void close() {
if (resultSet != null) {
@ -105,6 +105,14 @@ public class JDBCsetUp {
}
}
if (preparedStatement != null) {
try {
preparedStatement.close();
} catch (SQLException e) {
LOGGER.error("Error closing PreparedStatement", e);
}
}
if (connection != null) {
try {
connection.close();