Find if an SQLException was thrown because of a duplicate

前端 未结 9 2590
青春惊慌失措
青春惊慌失措 2021-02-19 11:01

I have a Java program that is agnostic from the database and I need to know, while inserting, if an SQLException was thrown because of a duplicate key.

If I was using a

9条回答
  •  梦毁少年i
    2021-02-19 11:46

    If you are using spring, set your unique KEY name as UK_user_id for example and catch the DataIntegrityViolationException.
    You can then compare ex.getCause().getConstraintName() with UK_user_id.

提交回复
热议问题