Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/378.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用try with resources/close this";“准备好的报表”;在“a”中;最后";条款_Java_Spring_Jdbc - Fatal编程技术网

Java 使用try with resources/close this";“准备好的报表”;在“a”中;最后";条款

Java 使用try with resources/close this";“准备好的报表”;在“a”中;最后";条款,java,spring,jdbc,Java,Spring,Jdbc,我正在sonarqube上运行我的JDBC代码。我的代码有问题 try (final Connection connection = DatabaseConnectionProvider.createConnection(configuration)) { PreparedStatement statement = connection.prepareStatement( "SELECT 1 FROM `todo_items` WHERE

我正在sonarqube上运行我的JDBC代码。我的代码有问题

try (final Connection connection = DatabaseConnectionProvider.createConnection(configuration)) {
        PreparedStatement statement = connection.prepareStatement(
                "SELECT 1 FROM `todo_items` WHERE `id` = ? LIMIT 1;");
        statement.setLong(1, id);
        ResultSet resultSet = statement.executeQuery();
        if (!resultSet.next()) {
            return false;
        }
        statement = connection.prepareStatement("UPDATE `todo_items` SET `checked` = ? WHERE id = ?;");
        statement.setBoolean(1, checked);
        statement.setLong(2, id);
        statement.executeUpdate();
        return true;
    }
它说在3号线和9号线有拦截器问题


使用try with resources或在“finally”子句中关闭此“PreparedStatement”。我不明白这一点。

您可以使用内部try with resources,因为您在内部有两个不同的prepared语句:

try (final Connection connection = DatabaseConnectionProvider.createConnection(configuration)) {
    try(PreparedStatement statement = connection.prepareStatement("SELECT 1 FROM `todo_items` WHERE `id` = ? LIMIT 1;")) {
        statement.setLong(1, id);
        ResultSet resultSet = statement.executeQuery();
        if (!resultSet.next()) {
            return false;
        }
    }
    try(PreparedStatement statement = connection.prepareStatement("UPDATE `todo_items` SET `checked` = ? WHERE id = ?;")) {
        statement.setBoolean(1, checked);
        statement.setLong(2, id);
        statement.executeUpdate();
        return true;
    }
}
小优化是可能的(请注意,
try(…)
中存在2个可自动关闭的s):


您不必查询数据库中是否存在行,因为更新的行数通常由
executeUpdate()
返回。实际返回值可能取决于JDBC驱动程序的实现(尽管JDBC规范对此非常清楚).

您已经有了一个连接的
try with resource
。请将准备好的语句也放在其中。如果您尝试JdbcTemplate,您可以用更少的样板文件来解决这个问题。您已经在使用Spring。我对您为什么不使用该类感到不解。您应该将数据源和Instance集中起来启动时在SpringBean工厂中初始化池。
try (final Connection connection = DatabaseConnectionProvider.createConnection(configuration);
        PreparedStatement statement = connection.prepareStatement("UPDATE `todo_items` SET `checked` = ? WHERE id = ?;")) {
    statement.setBoolean(1, checked);
    statement.setLong(2, id);
    return statement.executeUpdate() > 0;
}