【发布时间】:2020-03-23 16:04:24
【问题描述】:
假设我有这个:
String sql = "DELETE FROM "+ TABLE_NAME +" WHERE id=?";
try {
int ra = jdbcTemplate.update(connection -> {
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setLong(1, id);
stmt.executeUpdate();
return stmt;
});
if (ra == 0)
throw new SQLException(SQL_ERROR);
} catch (SQLException e){
LOGGER.error(SQL_ERROR);
throw new DataAccessLayerException("Entity could not be deleted due to SQL Exception");
}
我正在使用 key(id) 从表中删除一个实体。现在我希望能够向客户报告INTERNAL_SERVER_ERROR 而不会导致RuntimeException。
但是,我无法重现 SQLException。例如,如果我在我的 sql 语句中删除一个字母,我只会得到一个作为运行时错误抛出的语法错误,而且似乎我没有抓住它。
帮助我了解 SQLException 的确切含义以及如何重现它并将其报告给客户端而不导致 RuntimeException?
【问题讨论】:
标签: java sql spring-boot jdbc jdbctemplate