英文:
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<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();
});
}
});
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论