Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/390.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/12.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 错误:语句关闭后不允许任何操作_Java_Sql_Connection_Prepared Statement - Fatal编程技术网

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

Java 错误:语句关闭后不允许任何操作,java,sql,connection,prepared-statement,Java,Sql,Connection,Prepared Statement,我正在尝试执行此代码,但我有此错误,我知道我必须关闭与的连接 preparedStatement.close() 但是在处理完成之前它就关闭了,它给了我以下的错误 Caused by: java.sql.SQLException: No operations allowed after statement closed. 我的代码 private void fakesAssociate(List<Map<String, Object>> Employees)

我正在尝试执行此代码,但我有此错误,我知道我必须关闭与的连接

preparedStatement.close() 
但是在处理完成之前它就关闭了,它给了我以下的错误

Caused by: java.sql.SQLException: No operations allowed after statement closed.
我的代码

    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();

            });
        }
    });
}
private void fakessociate(列出员工){
coreEmployees.forEach(第->{
试一试{
myrefJdbcTemplate.update(“插入'n_associate'('active','first_name','birth')值(?,,?);”,preparedStatement->{
准备好的报表。setBoolean(1,正确);
preparedStatement.setString(2,String.valueOf(row.get(“常用名”));
Date birthdate=((Date)row.get(“birthdate”);
如果(生日!=null){
日历=Calendar.getInstance();
日历设置时间(生日);
日历集(日历年,1970年);
setDate(3,新日期(calendar.getTime().getTime());
}否则{
preparedStatement.setDate(3,空);
}
preparedStatement.close();
});
});
}捕获(DataIntegrityViolationException e){
字符串queryAssociate=“更新n\u关联集`active`=?”+
其中n_associate.first_name=?;“;
myrefJdbcTemplate.update(queryAssociate,preparedStatement->{
准备好的声明。setBoolean(1,假);
preparedStatement.close();
});
}
});
}

请帮助了解如何解决此问题。

不要关闭该语句。JdbcTemplate需要打开它才能执行它。JdbcTemplate将在完成使用后负责关闭该语句。

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

是的,我添加了它,因为我有一个内存泄漏错误'java.lang.OutOfMemoryError:java heap space'当我做一些研究时,我发现我必须关闭连接才能释放一些内存。你被误传了;OOME和关闭资源互不相关。@marie你需要投资t说明堆内存耗尽的原因,很可能不是这样。是的,我添加它是因为我有一个内存泄漏错误'java.lang.OutOfMemoryError:java heap space',当我进行一些研究时,我发现我必须关闭连接才能释放一些内存。你被错误地告知了;OOME和关闭资源与此无关彼此。@marie您需要调查堆内存耗尽的原因,很可能不是这样。