Thanks, Dennys.
I think that is SQLException.getErrorCode(). ;)
But this didn't resolve my problem, but the following code do! They only applies in exceptions of the type ConstraintViolationException.
Code:
import java.sql.SQLException;
import org.hibernate.exception.ConstraintViolationException;
/**
*
* @author Reinaldo de Oliveira Castro
*/
public abstract class SqlUtility {
public final static String UNIQUE_VIOLATION = "23505";
public static String getSQLState(ConstraintViolationException $cve) {
Throwable[] errs = ((ConstraintViolationException) $cve).getThrowables();
return ((SQLException) errs[errs.length - 1]).getSQLState();
}
}
And in my Main program:
Code:
if (e instanceof ConstraintViolationException) {
String SQLState = SqlUtility.getSQLState((ConstraintViolationException) e);
if ((SqlUtility.UNIQUE_VIOLATION).equals(SQLState))
System.out.println("It works!!!");
}
Thanks again,
Reinaldo de Oliveira Castro.