gpt4 book ai didi

java - 如何使用 Derby SQLState 常量

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:30:01 25 4
gpt4 key购买 nike

我的 Java 应用程序正在与 Apache Derby 10.8.1.2 数据库通信。我捕获了 SQL 异常并想针对特定情况进行测试:LANG_DUPLICATE_KEY_CONSTRAINT我找不到合适的 .jar 来导入据称包含此常量的 org.apache.derby.shared.common.reference

甚至 Derby Developer's Guide 也建议像这样进行测试:

if ( SQLState.equals( "23505" ) ) 

问题是,为什么要测试静态字符串而不是常量?我希望是这样的:

if ( SQLState.equals( SQLState.LANG_DUPLICATE_KEY_CONSTRAINT ) ) 

最佳答案

你可能不喜欢这个答案......但它是:

SqlState 故意不包含在任何 jar 文件中,以尽量减少 Derby 的大小。引用包说明:

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.

关于java - 如何使用 Derby SQLState 常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15495892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com