首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >错误:语句关闭后不允许执行任何操作

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

Stack Overflow用户
提问于 2020-04-09 18:54:25
回答 1查看 281关注 0票数 0

我试图执行这段代码,但我有这个错误,我知道我必须关闭连接

代码语言:javascript
运行
复制
preparedStatement.close() 

但是它在处理完成之前就关闭了,它给出了下面的错误

代码语言:javascript
运行
复制
Caused by: java.sql.SQLException: No operations allowed after statement closed.

我的代码

代码语言:javascript
运行
复制
    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();

            });
        }
    });
}

请帮助找出如何解决它。

EN

回答 1

Stack Overflow用户

发布于 2020-04-09 19:03:43

不要关闭该语句。JdbcTemplate需要打开它才能执行它。当使用完语句时,JdbcTemplate将负责关闭该语句。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61119507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档