我怎样才能捕捉到这个异常:
com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException:
Duplicate entry '22-85' for key 'ID_CONTACT'
最佳答案
我用的是spring,所以我们通过org.springframework.dao.DataIntegrityViolationException
try {
ao_history_repository.save(new AoHistory(..));
} catch (DataIntegrityViolationException e) {
System.out.println("history already exist");
}
但是作为 @KevinGuancheDarias提一下:
Please note that while this works. I suggest to solve the problem by issuing a findBy before the save, as this is messy, and I think it's not warranted that it will work in future versions, may even break without notification.
关于java - 捕获重复条目异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27582757/