简体   繁体   中英

How to use Derby SQLState constants

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 ) ) 

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 (eg new Integer(1)) or the final field is a calculated value that could not be resolved to a constant by the Java compiler.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM