java SQLException.getSQLState 的所有可能值是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1399574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What are all the possible values for SQLException.getSQLState?
提问by
SQLException.getSQLStateretrieves the SQLStatefor the SQLExceptionobject. What are all the possible values that can be returned by this method? Can I use the value to identify specific errors that ocured in the database (i.e. can this value tell me if it was a PK violation, or a unique constraint, or column value to large etc)?
SQLException.getSQLState检索SQLState的SQLException对象。此方法可以返回的所有可能值是什么?我是否可以使用该值来识别数据库中发生的特定错误(即该值是否可以告诉我它是否是 PK 违规、唯一约束或大列值等)?
Also, the DatabaseMetaData.getSQLStateType()method is supposed to indicate whether the SQLSTATEreturned by SQLException.getSQLStateis X/Open (now known as Open Group) SQL CLI or SQL99. The only possible value for this should be DatabaseMetaData.sqlStateXOpen == 1and DatabaseMetaData.sqlStateSQL99 == 2but I am getting the value 0. Am I missing something?
此外,该DatabaseMetaData.getSQLStateType()方法应该指示SQLSTATE返回的SQLException.getSQLState是 X/Open(现在称为 Open Group)SQL CLI 还是 SQL99。唯一可能的价值应该是DatabaseMetaData.sqlStateXOpen == 1,DatabaseMetaData.sqlStateSQL99 == 2但我得到了价值0。我错过了什么吗?
Is there a way that I can determine the specific type of error that occurred in the DB using combinations from the above mentioned methods? Can I count on the values of SQLException.getSQLState? Are these values different from DB provider to DB provider?
有没有一种方法可以使用上述方法的组合来确定数据库中发生的特定错误类型?我可以指望 的值SQLException.getSQLState吗?这些值是否因数据库提供程序而异?
采纳答案by madcolor
Official documents that include SQLStates can obviously be purchased, at a relatively high price, from ANSI and XOpen. But, the documentation for most databases have lists of SQLStates. Probably the most complete ( and accessible ) online listings are in the DB2 manuals. Check the DB2 Universal Messages manual, for instance. Oracle ( TechNet password required ) and Sybase, among others, also have online listings.
包含 SQLStates 的官方文档显然可以从 ANSI 和 XOpen 以相对较高的价格购买。但是,大多数数据库的文档都有 SQLState 列表。最完整(和可访问)的在线列表可能在 DB2 手册中。例如,查看DB2 Universal Messages 手册。Oracle(需要 TechNet 密码)和 Sybase 等也有在线列表。
As to the second question, this is the intent of SQLState, however, the various databases have varying degrees of compliance. For example, some map multiple native error messages to the same SQLState. For generic use, one should probably concentrate on the major code ( the first two characters of SQLState, ) then determine if more specific info is available in the minor code ( beyond 000. )
关于第二个问题,这是SQLState的本意,但是各个数据库的合规程度各不相同。例如,有些将多个本机错误消息映射到同一个 SQLState。对于一般用途,我们应该专注于主要代码(SQLState 的前两个字符,),然后确定次要代码(000 之后)中是否有更具体的信息可用。
回答by Amit Jain
MySQL provides some info in its reference manual in Server Error Codes and Messages
MySQL 在服务器错误代码和消息的参考手册中提供了一些信息
回答by Simon Kissane
The X/Open (now Open Group) standards are now available free of charge (but registration required). See Data Management: SQL Call Level Interface (CLI)for the SQL CLI (aka ODBC) standard, SQLSTATE codes are defined in Appendix A. And Data Management: Structured Query Language (SQL), Version 2for the SQL standard, SQLSTATE codes are defined in Appendix B. As for the ISO standards, while you have to pay for the official versions, the final drafts (which are almost identical) are freely available; for SQL:2011, see page 2017 (by PDF numbering; 1194 by document page numbering) of Final Committee Draft ISO/IEC FCD 9075-2
X/Open(现在是 Open Group)标准现在免费提供(但需要注册)。请参阅数据管理:SQLCLI(又名 ODBC)标准的SQL 调用级接口 (CLI),SQLSTATE 代码在附录 A 中定义。而数据管理:结构化查询语言 (SQL),SQL 标准的版本 2,SQLSTATE 代码是在附录 B 中定义。对于 ISO 标准,虽然您必须为正式版本付费,但最终草案(几乎相同)是免费提供的;对于 SQL:2011,请参阅ISO/IEC FCD 9075-2 最终委员会草案的第 2017 页(按 PDF 编号;按文档页码编号为 1194)
回答by skaffman
This is to some degree JDBC driver-dependent. There do seem to be standard values, plus some proprietary values.
这在某种程度上取决于 JDBC 驱动程序。似乎确实存在标准值,以及一些专有值。
As a guide to what's possible, Spring's JDBC layer includes SQL error code and state translation. It provides a SQLState translatorwhich gives rather vague exception translation, as well as a SQLErrorCode translatorwhich is much more fine-grained, using known proprietary error codes.
作为可能的指南,Spring 的 JDBC 层包括 SQL 错误代码和状态转换。它提供了一个SQLState 翻译器,它提供了相当模糊的异常翻译,以及一个使用已知专有错误代码的更细粒度的SQLErrorCode 翻译器。
If you can't use Spring, then download the source code, and extract the sql-error-codes.xmlfile, which contains the mapping from codes to exception types.
如果您不能使用 Spring,则下载源代码,并解压缩sql-error-codes.xml文件,其中包含从代码到异常类型的映射。

