前言:
有时候在数据库表中,对某列设置了唯一约束,
在对某条数据,进行修改或者新增时候,程序中会抛出异常: SQLIntegrityConstraintViolationException
简单处理:
try {
//修改or新增sql
}catch (Exception e){
Throwable cause = e.getCause();
if (cause instanceof SQLIntegrityConstraintViolationException){
String errMsg = (cause).getMessage();
if (StringUtils.isNotEmpty(errMsg) && errMsg.contains("idx_tel")){
throw new ApiException(500,"手机号已经存在");
}
if (StringUtils.isNotEmpty(errMsg) && errMsg.contains("idx_account")){
throw new ApiException(500,"账号已经存在");
}
}
throw new ApiException(500,参数错误);
}