简体   繁体   English

如何使用Derby SQLState常量

[英]How to use Derby SQLState constants

My Java app is communicating with Apache Derby 10.8.1.2 DB. 我的Java应用程序正在与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. 我捕获SQL异常并想测试一个特定的情况: LANG_DUPLICATE_KEY_CONSTRAINT我无法找到正确的.jar来导入org.apache.derby.shared.common.reference ,它应该包含这个常量。

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. 故意不将SqlState包含在任何jar文件中,以尽量减小Derby的大小。 Quoting from the package description: 引用包描述:

Package org.apache.derby.shared.common.reference Description 包org.apache.derby.shared.common.reference描述

Useful constants that the Java compiler will in-line. Java编译器将在线的有用常量。 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. 这些常量将由Java编译器内联,希望导致占用空间更小,而不是对字段的引用。 Fields in interfaces are automatically public, static and final, hence constants. 接口中的字段自动是public,static和final,因此是常量。

These classes should not need to be shipped with any of the Derby jar files. 这些类不需要随任何Derby jar文件一起提供。 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. 如果一个随附Jar文件,则很可能意味着表示常量的字段是非String对象(例如new Integer(1))或者final字段是无法通过其解析为常量的计算值。 Java编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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