如果你有一个JdbcTemplate
,你可以做
catch (SqlException e) {
throw jdbcTemplate.getExceptionTranslator().translate("my task", null, e);
}
如果没有JdbcTemplate
,直接看方法的源码JdbcTemplate.getExceptionTranslator()
:
public synchronized SQLExceptionTranslator getExceptionTranslator() {
if (this.exceptionTranslator == null) {
DataSource dataSource = getDataSource();
if (dataSource != null) {
this.exceptionTranslator = new SQLErrorCodeSQLExceptionTranslator(dataSource);
}
else {
this.exceptionTranslator = new SQLStateSQLExceptionTranslator();
}
}
return this.exceptionTranslator;
}
并模仿它的行为:-)