Find if an SQLException was thrown because of a duplicate

前端 未结 9 2592
青春惊慌失措
青春惊慌失措 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条回答
  •  说谎
    说谎 (楼主)
    2021-02-19 11:35

    This is exactly what SQLException.getSQLState() is for. Acoording to Google, "23000" indicates a unique constraint violation in at least MySQL, PostgreSQL, and Oracle.

提交回复
热议问题