Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Derby SQLState constants

Tags:

java

derby

My Java app is communicating with Apache Derby 10.8.1.2 DB. I catch SQL exception and want to test for a specific case: LANG_DUPLICATE_KEY_CONSTRAINT I was not able to find proper .jar to import org.apache.derby.shared.common.reference that supposedly contains this constant.

Even Derby Developer's Guide suggests test like:

if ( SQLState.equals( "23505" ) ) 

Question is, why test for a static string instead of a constant? I would expect something like:

if ( SQLState.equals( SQLState.LANG_DUPLICATE_KEY_CONSTRAINT ) ) 
like image 778
stuhpa Avatar asked Dec 06 '25 03:12

stuhpa


1 Answers

You may not like the answer ... but here it is:

SqlState was deliberately not included in any of the jar files, to try to minimize the size of Derby. Quoting from the package description:

Package org.apache.derby.shared.common.reference Description

Useful constants that the Java compiler will in-line. A collection of reference interfaces that contain primitive or String constants. Such constants will be in-lined by the Java compiler hopefully leading to smaller footprint, than a reference to a field entails. Fields in interfaces are automatically public, static and final, hence constants.

These classes should not need to be shipped with any of the Derby jar files. If one is shipped with a Jar file then it most likely means the field representing the constant was a non-String Object (e.g. new Integer(1)) or the final field is a calculated value that could not be resolved to a constant by the Java compiler.

like image 192
Steve Avatar answered Dec 08 '25 16:12

Steve



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!