In both cases of PrimaryKey violation and uniqueKey violation, I see the same message, error code and sql state values.
When I run the insert query directly in the db, getting the same error message for both the cases. Only difference is the constraint name. we cannot rely on the constraint name all the time. How to differentiate the different types of SQL constraints violations?
try {
// create Db connection & sql insert into table statement
// stmt.executeUpdate("insert into mytable (A,B) .......")
// mytable has primary key on column A and unique key on column B
}catch(Exception e){
System.out.println(">>>>>>>>>> get Message :: " + e.getMessage());
System.out.println(">>>>>>>>>> get err code:: " + ((SQLException)e).getErrorCode());
System.out.println(">>>>>>>>>> get sql state:: " + ((SQLException)e).getSQLState());
}
Test 1:
>>>>>>>>>> get Message :: ORA-00001: unique constraint (TABLE_NAME.UK) violated
>>>>>>>>>> get err code:: 1
>>>>>>>>>> get sql state:: 23000
Test 2:
>>>>>>>>>> get Message :: ORA-00001: unique constraint (TABLE_NAME.PK) violated
>>>>>>>>>> get err code:: 1
>>>>>>>>>> get sql state:: 23000