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

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

error : No operations allowed after statement closed

问题

  1. 我正在尝试执行这段代码,但是我遇到了这个错误,我知道我需要在以下位置关闭连接:
  2. preparedStatement.close()
  3. 但是它在处理完成之前就关闭了,导致了以下错误:
  4. Caused by: java.sql.SQLException: 在语句关闭后不允许执行操作。
  5. 我的代码:
  6. private void fakesAssociate(List<Map<String, Object>> Employees) {
  7. coreEmployees.forEach(row -> {
  8. try {
  9. myrefJdbcTemplate.update("INSERT INTO `n_associate` (`active`, `first_name`, `birthday`) VALUES ( ?, ?, ? );", preparedStatement -> {
  10. preparedStatement.setBoolean(1, true);
  11. preparedStatement.setString(2, String.valueOf(row.get("usual_first_name")));
  12. Date birthdate = ((Date) row.get("birth_date"));
  13. if (birthdate != null) {
  14. Calendar calendar = Calendar.getInstance();
  15. calendar.setTime(birthdate);
  16. calendar.set(Calendar.YEAR, 1970);
  17. preparedStatement.setDate(3, new Date(calendar.getTime().getTime()));
  18. } else {
  19. preparedStatement.setDate(3, null);
  20. }
  21. preparedStatement.close();
  22. });
  23. } catch (DataIntegrityViolationException e) {
  24. String queryAssociate = "UPDATE n_associate SET `active`= ? WHERE n_associate.first_name = ?;";
  25. myrefJdbcTemplate.update(queryAssociate, preparedStatement -> {
  26. preparedStatement.setBoolean(1, false);
  27. preparedStatement.close();
  28. });
  29. }
  30. });
  31. }

请帮忙找出解决方法。

英文:

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

  1. preparedStatement.close()

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

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

my code

  1. private void fakesAssociate(List&lt;Map&lt;String, Object&gt;&gt; Employees) {
  2. coreEmployees.forEach(row -&gt; {
  3. try {
  4. myrefJdbcTemplate.update(&quot;INSERT INTO `n_associate` (`active`, `first_name`, `birthday`) VALUES ( ?, ?, ? );&quot;, preparedStatement -&gt; {
  5. preparedStatement.setBoolean(1, true);
  6. preparedStatement.setString(2, String.valueOf(row.get(&quot;usual_first_name&quot;)));
  7. Date birthdate = ((Date) row.get(&quot;birth_date&quot;));
  8. if (birthdate != null) {
  9. Calendar calendar = Calendar.getInstance();
  10. calendar.setTime(birthdate);
  11. calendar.set(Calendar.YEAR, 1970);
  12. preparedStatement.setDate(3, new Date(calendar.getTime().getTime()));
  13. } else {
  14. preparedStatement.setDate(3, null);
  15. }
  16. preparedStatement.close();
  17. });
  18. });
  19. } catch (DataIntegrityViolationException e) {
  20. String queryAssociate = &quot;UPDATE n_associate SET `active`= ? &quot; +
  21. WHERE n_associate.first_name = ?;&quot;;
  22. myrefJdbcTemplate.update(queryAssociate, preparedStatement -&gt; {
  23. preparedStatement.setBoolean(1, false);
  24. preparedStatement.close();
  25. });
  26. }
  27. });
  28. }

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:

确定