错误:语句关闭后不允许进行任何操作

huangapple go评论76阅读模式
英文:

error : No operations allowed after statement closed

问题

我正在尝试执行这段代码,但是我遇到了这个错误,我知道我需要在以下位置关闭连接:

    preparedStatement.close()

但是它在处理完成之前就关闭了,导致了以下错误:

    Caused by: java.sql.SQLException: 在语句关闭后不允许执行操作。

我的代码:

private void fakesAssociate(List<Map<String, Object>> Employees) {
    coreEmployees.forEach(row -> {
        try {
            myrefJdbcTemplate.update("INSERT INTO `n_associate` (`active`, `first_name`, `birthday`) VALUES ( ?, ?, ? );", preparedStatement -> {
                preparedStatement.setBoolean(1, true);
                preparedStatement.setString(2, String.valueOf(row.get("usual_first_name")));
                
                Date birthdate = ((Date) row.get("birth_date"));
                if (birthdate != null) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(birthdate);
                    calendar.set(Calendar.YEAR, 1970);
                    preparedStatement.setDate(3, new Date(calendar.getTime().getTime()));
                } else {
                    preparedStatement.setDate(3, null);
                }

                preparedStatement.close();
            });
        } catch (DataIntegrityViolationException e) {
            String queryAssociate = "UPDATE n_associate SET  `active`= ? WHERE n_associate.first_name = ?;";

            myrefJdbcTemplate.update(queryAssociate, preparedStatement -> {
                preparedStatement.setBoolean(1, false);
                preparedStatement.close();
            });
        }
    });
}

请帮忙找出解决方法。

英文:

I'm trying to excute this code but I have this error , I know I have to close the connection with

preparedStatement.close() 

but it's closed before the treatement is finished and it gives me the following error

Caused by: java.sql.SQLException: No operations allowed after statement closed.

my code

    private void fakesAssociate(List&lt;Map&lt;String, Object&gt;&gt; Employees) {
    coreEmployees.forEach(row -&gt; {
        try {
            myrefJdbcTemplate.update(&quot;INSERT INTO `n_associate` (`active`, `first_name`, `birthday`) VALUES ( ?, ?, ? );&quot;, preparedStatement -&gt; {
                   preparedStatement.setBoolean(1, true);
                   preparedStatement.setString(2, String.valueOf(row.get(&quot;usual_first_name&quot;)));
                
                Date birthdate = ((Date) row.get(&quot;birth_date&quot;));
                if (birthdate != null) {
                    Calendar calendar = Calendar.getInstance();
                    calendar.setTime(birthdate);
                    calendar.set(Calendar.YEAR, 1970);
                    preparedStatement.setDate(3, new Date(calendar.getTime().getTime()));
                } else {
                    preparedStatement.setDate(3, null);
                }

                preparedStatement.close();
            });

            });
        } catch (DataIntegrityViolationException e) {
            String queryAssociate = &quot;UPDATE n_associate SET  `active`= ?  &quot; +
                WHERE n_associate.first_name = ?;&quot;;

            myrefJdbcTemplate.update(queryAssociate, preparedStatement -&gt; {
     
                    preparedStatement.setBoolean(1, false);
                preparedStatement.close();

            });
        }
    });
}

please help to find out how to solve it

答案1

得分: 2

不要关闭这个语句。JdbcTemplate 需要它保持打开状态,以便执行。当 JdbcTemplate 完成使用后,它会负责关闭这个语句。

英文:

Don't close the statement. JdbcTemplate will need it open so it can execute it. JdbcTemplate will take care of closing the statement when it's finished using it.

huangapple
  • 本文由 发表于 2020年4月9日 18:54:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/61119507.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定