SQLException error code

Sometimes you would want to base your decisions in your java code on the error code you get from the SQLException. Now unfortunately these error codes are vendor specific and are different for each vendor, e.g.

"unique constraint"

Oracle:: 1

MySQL:: 2300

Hypersonic:: 104

Now this raises a question as to how to handle such a situation? you dont want to change the code if the underlying database changes or if you are connecting to more than one DB. In our case we use hypersonic for unit tests and Oracle for actual deployment. Does the jdbcTemplate provide us with any help in this scenario?

Message was edited by:

kilyas

[677 byte] By [kilyasa] at [2007-11-26 21:55:48]
# 1

> Now this raises a question as to how to handle such a

> situation?

By looking at the SQLState string. See class docs for SQLException: http://java.sun.com/j2se/1.5.0/docs/api/java/sql/SQLException.html

However, there's two possible formats, and some DBMS's (Oracle!) don't tell you which they use.

So it's probably better to implement a "dialect" object for each DBMS that you actually use, and look at the actual error code.

kdgregorya at 2007-7-10 3:51:55 > top of Java-index,Java Essentials,Java Programming...
# 2

how about something like?

if ("HSQL Database Engine Driver".equals(driverName) && e.getErrorCode() == -104) {

log.info("Duplicateinserting .....");

}else if("Oracle JDBC Driver".equals(driverName) && e.getErrorCode() == 1){

log.info("Duplicateinserting...");

}

kilyasa at 2007-7-10 3:51:55 > top of Java-index,Java Essentials,Java Programming...