java 如何在 JDBC 中捕获特定异常?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1988570/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 18:50:50  来源:igfitidea点击:

How to catch a specific exception in JDBC?

javajdbc

提问by GuruKulki

How to catch a specific exceptions in JDBC? Examples: primary key exception or foreign key exception.

如何捕获JDBC 中的特定异常?示例:主键异常或外键异常。

采纳答案by Brian Agnew

SQLExceptioncontains some database-specific info related to the exception. From the doc:

SQLException包含一些与异常相关的特定于数据库的信息。从文档:

Each SQLException provides several kinds of information:

1) a string describing the error. This is used as the Java Exception message, available via the method getMesage.

2) a "SQLstate" string, which follows either the XOPEN SQLstate conventions or the SQL 99 conventions. The values of the SQLState string are described in the appropriate spec. The DatabaseMetaData method getSQLStateType can be used to discover whether the driver returns the XOPEN type or the SQL 99 type.

3) an integer error code that is specific to each vendor. Normally this will be the actual error code returned by the underlying database.

4) a chain to a next Exception. This can be used to provide additional error information.

每个 SQLException 提供多种信息:

1) 描述错误的字符串。这用作 Java 异常消息,可通过 getMesage 方法获得。

2) 一个“SQLstate”字符串,它遵循 XOPEN SQLstate 约定或 SQL 99 约定。SQLState 字符串的值在相应的规范中进行了描述。DatabaseMetaData 方法 getSQLStateType 可用于发现驱动程序返回的是 XOPEN 类型还是 SQL 99 类型。

3) 特定于每个供应商的整数错误代码。通常这将是底层数据库返回的实际错误代码。

4) 到下一个异常的链。这可用于提供额外的错误信息。

回答by BalusC

The best and DB-independent way to handle SQLExceptionmore specifically is to determine the SQL state code which can be obtained by SQLException#getSQLState(). The SQLState is a 5-char code, of which the first two are common among all DB's and the last three might differ depending on the DB and/or the specific condition. Here's an extract from the spec:

最好的、独立于 DB 的处理SQLException更具体的方法是确定可以通过SQLException#getSQLState(). SQLState 是一个 5 个字符的代码,其中前两个在所有数据库中通用,后三个可能因数据库和/或特定条件而异。这是规范的摘录:

  • 02: no data
  • 07: dynamic SQL error
  • 08: connection exception
  • 0A: feature not supported
  • 21: cardinality violation
  • 22: data exception
  • 23: integrity constraint violation
  • 24: invalid cursor state
  • 25: invalid transaction state
  • 26: invalid SQL statement name
  • 28: invalid authorization specification
  • 2B: dependent privilege descriptors still exist
  • 2C: invalid character set name
  • 2D: invalid transaction termination
  • 2E: invalid connection name
  • 33: invalid SQL descriptor name
  • 34: invalid cursor name
  • 35: invalid condition number
  • 3C: ambiguous cursor name
  • 3D: invalid catalog name
  • 3F: invalid schema name
  • 02:无数据
  • 07:动态SQL错误
  • 08:连接异常
  • 0A:不支持功能
  • 21:违反基数
  • 22:数据异常
  • 23:违反完整性约束
  • 24:无效的游标状态
  • 25:无效的交易状态
  • 26:无效的 SQL 语句名称
  • 28:授权规范无效
  • 2B:依赖的特权描述符仍然存在
  • 2C:无效的字符集名称
  • 2D:无效交易终止
  • 2E:无效的连接名称
  • 33:无效的 SQL 描述符名称
  • 34:无效的游标名称
  • 35:无效的条件号
  • 3C:不明确的游标名称
  • 3D:无效的目录名称
  • 3F:无效的架构名称

So to determine whether the SQL Exception is caused by a constraint violation, you can just do the following in a (fictive) SQLUtilclass:

因此,要确定 SQL 异常是否是由违反约束引起的,您只需在(虚构的)SQLUtil类中执行以下操作:

public static boolean isConstraintViolation(SQLException e) {
    return e.getSQLState().startsWith("23");
}

回答by Kaleb Brasee

Brian's right, a SQLException will be thrown for just about ANY JDBC problem. This is partially why JDBC is so annoying. The Spring library JDBC helpersprovide an exception translatorto look at the SQLCode, SQLState, etc., and throw the appropriate DataAccessException. There are many of these exception classes, and they give you a better idea of what went wrong, with names such as DataIntegrityViolationException, DataSourceLookupFailureException, PermissionDeniedDataAccessException, and others.

布赖恩是对的,几乎任何 JDBC 问题都会抛出 SQLException。这就是 JDBC 如此烦人的部分原因。在春季库JDBC助手提供了一个异常解释一下,SQLCode将和SQLState等,并引发相应的DataAccessException。有许多这样的异常类,它们让您更好地了解出了什么问题,名称有 DataIntegrityViolationException、DataSourceLookupFailureException、PermissionDeniedDataAccessException 等。

回答by Lukas Eder

Following up on BalusC's answer, here's a more complete, recent list of all the classes and subclasses as specified by the SQL:2011 standard. I have just recently assembled this list for the Javadoc of jOOQ's SQLStateSubclass.

跟进BalusC 的回答,这是 SQL:2011 标准指定的所有类和子类的更完整的最新列表。我最近刚刚为 jOOQ 的 JavadocSQLStateSubclass汇编了这个列表。

+----+-----------------------------------------------------------+-----+--------------------------------------------------------------+
| Class and class description                                    | Subclass and subclass description                                  |
+----+-----------------------------------------------------------+-----+--------------------------------------------------------------+
| 00 | Successful completion                                     | 000 | No subclass                                                  |
| 01 | Warning                                                   | 000 | No subclass                                                  |
| 01 | Warning                                                   | 001 | Cursor operation conflict                                    |
| 01 | Warning                                                   | 002 | Disconnect error                                             |
| 01 | Warning                                                   | 003 | Null value eliminated in set function                        |
| 01 | Warning                                                   | 004 | String data, right truncation                                |
| 01 | Warning                                                   | 005 | Insufficient item descriptor areas                           |
| 01 | Warning                                                   | 006 | Privilege not revoked                                        |
| 01 | Warning                                                   | 007 | Privilege not granted                                        |
| 01 | Warning                                                   | 009 | Search condition too long for information schema             |
| 01 | Warning                                                   | 00A | Query expression too long for information schema             |
| 01 | Warning                                                   | 00B | Default value too long for information schema                |
| 01 | Warning                                                   | 00C | Result sets returned                                         |
| 01 | Warning                                                   | 00D | Additional result sets returned                              |
| 01 | Warning                                                   | 00E | Attempt to return too many result sets                       |
| 01 | Warning                                                   | 00F | Statement too long for information schema                    |
| 01 | Warning                                                   | 012 | Invalid number of conditions                                 |
| 01 | Warning                                                   | 02F | Array data, right truncation                                 |
| 02 | No data                                                   | 000 | No subclass                                                  |
| 02 | No data                                                   | 001 | No additional result sets returned                           |
| 07 | Dynamic SQL Error                                         | 000 | No subclass                                                  |
| 07 | Dynamic SQL Error                                         | 001 | Using clause does not match dynamic parameter specifications |
| 07 | Dynamic SQL Error                                         | 002 | Using clause does not match target specifications            |
| 07 | Dynamic SQL Error                                         | 003 | Cursor specification cannot be executed                      |
| 07 | Dynamic SQL Error                                         | 004 | Using clause required for dynamic parameters                 |
| 07 | Dynamic SQL Error                                         | 005 | Prepared statement not a cursor specification                |
| 07 | Dynamic SQL Error                                         | 006 | Restricted data type attribute violation                     |
| 07 | Dynamic SQL Error                                         | 007 | Using clause required for result fields                      |
| 07 | Dynamic SQL Error                                         | 008 | Invalid descriptor count                                     |
| 07 | Dynamic SQL Error                                         | 009 | Invalid descriptor index                                     |
| 07 | Dynamic SQL Error                                         | 00B | Data type transform function violation                       |
| 07 | Dynamic SQL Error                                         | 00C | Undefined DATA value                                         |
| 07 | Dynamic SQL Error                                         | 00D | Invalid DATA target                                          |
| 07 | Dynamic SQL Error                                         | 00E | Invalid LEVEL value                                          |
| 07 | Dynamic SQL Error                                         | 00F | Invalid DATETIME_INTERVAL_CODE                               |
| 08 | Connection exception                                      | 000 | No subclass                                                  |
| 08 | Connection exception                                      | 001 | SQL-client unable to establish SQL-connection                |
| 08 | Connection exception                                      | 002 | Connection name in use                                       |
| 08 | Connection exception                                      | 003 | Connection does not exist                                    |
| 08 | Connection exception                                      | 004 | SQL-server rejected establishment of SQL-connection          |
| 08 | Connection exception                                      | 006 | Connection failure                                           |
| 08 | Connection exception                                      | 007 | Transaction resolution unknown                               |
| 09 | Triggered action exception                                | 000 | No subclass                                                  |
| 0A | Feature not supported                                     | 000 | No subclass                                                  |
| 0A | Feature not supported                                     | 001 | Multiple server transactions                                 |
| 0D | Invalid target type specification                         | 000 | No subclass                                                  |
| 0E | Invalid schema name list specification                    | 000 | No subclass                                                  |
| 0F | Locator exception                                         | 000 | No subclass                                                  |
| 0F | Locator exception                                         | 001 | Invalid specification                                        |
| 0L | Invalid grantor                                           | 000 | No subclass                                                  |
| 0M | Invalid SQL-invoked procedure reference                   | 000 | No subclass                                                  |
| 0P | Invalid role specification                                | 000 | No subclass                                                  |
| 0S | Invalid transform group name specification                | 000 | No subclass                                                  |
| 0T | Target table disagrees with cursor specification          | 000 | No subclass                                                  |
| 0U | Attempt to assign to non-updatable column                 | 000 | No subclass                                                  |
| 0V | Attempt to assign to ordering column                      | 000 | No subclass                                                  |
| 0W | Prohibited statement encountered during trigger execution | 000 | No subclass                                                  |
| 0W | Prohibited statement encountered during trigger execution | 001 | Modify table modified by data change delta table             |
| 0Z | Diagnostics exception                                     | 000 | No subclass                                                  |
| 0Z | Diagnostics exception                                     | 001 | Maximum number of stacked diagnostics areas exceeded         |
| 21 | Cardinality violation                                     | 000 | No subclass                                                  |
| 22 | Data exception                                            | 000 | No subclass                                                  |
| 22 | Data exception                                            | 001 | String data, right truncation                                |
| 22 | Data exception                                            | 002 | Null value, no indicator parameter                           |
| 22 | Data exception                                            | 003 | Numeric value out of range                                   |
| 22 | Data exception                                            | 004 | Null value not allowed                                       |
| 22 | Data exception                                            | 005 | Error in assignment                                          |
| 22 | Data exception                                            | 006 | Invalid interval format                                      |
| 22 | Data exception                                            | 007 | Invalid datetime format                                      |
| 22 | Data exception                                            | 008 | Datetime field overflow                                      |
| 22 | Data exception                                            | 009 | Invalid time zone displacement value                         |
| 22 | Data exception                                            | 00B | Escape character conflict                                    |
| 22 | Data exception                                            | 00C | Invalid use of escape character                              |
| 22 | Data exception                                            | 00D | Invalid escape octet                                         |
| 22 | Data exception                                            | 00E | Null value in array target                                   |
| 22 | Data exception                                            | 00F | Zero-length character string                                 |
| 22 | Data exception                                            | 00G | Most specific type mismatch                                  |
| 22 | Data exception                                            | 00H | Sequence generator limit exceeded                            |
| 22 | Data exception                                            | 00P | Interval value out of range                                  |
| 22 | Data exception                                            | 00Q | Multiset value overflow                                      |
| 22 | Data exception                                            | 010 | Invalid indicator parameter value                            |
| 22 | Data exception                                            | 011 | Substring error                                              |
| 22 | Data exception                                            | 012 | Division by zero                                             |
| 22 | Data exception                                            | 013 | Invalid preceding or following size in window function       |
| 22 | Data exception                                            | 014 | Invalid argument for NTILE function                          |
| 22 | Data exception                                            | 015 | Interval field overflow                                      |
| 22 | Data exception                                            | 016 | Invalid argument for NTH_VALUE function                      |
| 22 | Data exception                                            | 018 | Invalid character value for cast                             |
| 22 | Data exception                                            | 019 | Invalid escape character                                     |
| 22 | Data exception                                            | 01B | Invalid regular expression                                   |
| 22 | Data exception                                            | 01C | Null row not permitted in table                              |
| 22 | Data exception                                            | 01E | Invalid argument for natural logarithm                       |
| 22 | Data exception                                            | 01F | Invalid argument for power function                          |
| 22 | Data exception                                            | 01G | Invalid argument for width bucket function                   |
| 22 | Data exception                                            | 01H | Invalid row version                                          |
| 22 | Data exception                                            | 01S | Invalid XQuery regular expression                            |
| 22 | Data exception                                            | 01T | Invalid XQuery option flag                                   |
| 22 | Data exception                                            | 01U | Attempt to replace a zero-length string                      |
| 22 | Data exception                                            | 01V | Invalid XQuery replacement string                            |
| 22 | Data exception                                            | 01W | Invalid row count in fetch first clause                      |
| 22 | Data exception                                            | 01X | Invalid row count in result offset clause                    |
| 22 | Data exception                                            | 020 | Invalid period value                                         |
| 22 | Data exception                                            | 021 | Character not in reperttheitroade                                  |
| 22 | Data exception                                            | 022 | Indicator overflow                                           |
| 22 | Data exception                                            | 023 | Invalid parameter value                                      |
| 22 | Data exception                                            | 024 | Unterminated C string                                        |
| 22 | Data exception                                            | 025 | Invalid escape sequence                                      |
| 22 | Data exception                                            | 026 | String data, length mismatch                                 |
| 22 | Data exception                                            | 027 | Trim error                                                   |
| 22 | Data exception                                            | 029 | Noncharacter in UCS string                                   |
| 22 | Data exception                                            | 02D | Null value substituted for mutator subject parameter         |
| 22 | Data exception                                            | 02E | Array element error                                          |
| 22 | Data exception                                            | 02F | Array data, right truncation                                 |
| 22 | Data exception                                            | 02G | Invalid repeat argument in sample clause                     |
| 22 | Data exception                                            | 02H | Invalid sample size                                          |
| 23 | Integrity constraint violation                            | 000 | No subclass                                                  |
| 23 | Integrity constraint violation                            | 001 | Restrict violation                                           |
| 24 | Invalid cursor state                                      | 000 | No subclass                                                  |
| 25 | Invalid transaction state                                 | 000 | No subclass                                                  |
| 25 | Invalid transaction state                                 | 001 | Active SQL-transaction                                       |
| 25 | Invalid transaction state                                 | 002 | Branch transaction already active                            |
| 25 | Invalid transaction state                                 | 003 | Inappropriate access mode for branch transaction             |
| 25 | Invalid transaction state                                 | 004 | Inappropriate isolation level for branch transaction         |
| 25 | Invalid transaction state                                 | 005 | No active SQL-transaction for branch transaction             |
| 25 | Invalid transaction state                                 | 006 | Read-only SQL-transaction                                    |
| 25 | Invalid transaction state                                 | 007 | Schema and data statement mixing not supported               |
| 25 | Invalid transaction state                                 | 008 | Held cursor requires same isolation level                    |
| 26 | Invalid SQL statement name                                | 000 | No subclass                                                  |
| 27 | Triggered data change violation                           | 000 | No subclass                                                  |
| 27 | Triggered data change violation                           | 001 | Modify table modified by data change delta table             |
| 28 | Invalid authorization specification                       | 000 | No subclass                                                  |
| 2B | Dependent privilege descriptors still exist               | 000 | No subclass                                                  |
| 2C | Invalid character set name                                | 000 | No subclass                                                  |
| 2C | Invalid character set name                                | 001 | Cannot drop SQL-session default character set                |
| 2D | Invalid transaction termination                           | 000 | No subclass                                                  |
| 2E | Invalid connection name                                   | 000 | No subclass                                                  |
| 2F | SQL routine exception                                     | 000 | No subclass                                                  |
| 2F | SQL routine exception                                     | 002 | Modifying SQL-data not permitted                             |
| 2F | SQL routine exception                                     | 003 | Prohibited SQL-statement attempted                           |
| 2F | SQL routine exception                                     | 004 | Reading SQL-data not permitted                               |
| 2F | SQL routine exception                                     | 005 | Function executed no return statement                        |
| 2H | Invalid collation name                                    | 000 | No subclass                                                  |
| 30 | Invalid SQL statement identifier                          | 000 | No subclass                                                  |
| 33 | Invalid SQL descriptor name                               | 000 | No subclass                                                  |
| 34 | Invalid cursor name                                       | 000 | No subclass                                                  |
| 35 | Invalid condition number                                  | 000 | No subclass                                                  |
| 36 | Cursor sensitivity exception                              | 000 | No subclass                                                  |
| 36 | Cursor sensitivity exception                              | 001 | request rejected                                             |
| 36 | Cursor sensitivity exception                              | 002 | request failed                                               |
| 38 | External routine exception                                | 000 | No subclass                                                  |
| 38 | External routine exception                                | 001 | Containing SQL not permitted                                 |
| 38 | External routine exception                                | 002 | Modifying SQL-data not permitted                             |
| 38 | External routine exception                                | 003 | Prohibited SQL-statement attempted                           |
| 38 | External routine exception                                | 004 | Reading SQL-data not permitted                               |
| 39 | External routine invocation exception                     | 000 | No subclass                                                  |
| 39 | External routine invocation exception                     | 004 | Null value not allowed                                       |
| 3B | Savepoint exception                                       | 000 | No subclass                                                  |
| 3B | Savepoint exception                                       | 001 | Invalid specification                                        |
| 3B | Savepoint exception                                       | 002 | Too many                                                     |
| 3C | Ambiguous cursor name                                     | 000 | No subclass                                                  |
| 3D | Invalid catalog name                                      | 000 | No subclass                                                  |
| 3F | Invalid schema name                                       | 000 | No subclass                                                  |
| 40 | Transaction rollback                                      | 000 | No subclass                                                  |
| 40 | Transaction rollback                                      | 001 | Serialization failure                                        |
| 40 | Transaction rollback                                      | 002 | Integrity constraint violation                               |
| 40 | Transaction rollback                                      | 003 | Statement completion unknown                                 |
| 40 | Transaction rollback                                      | 004 | Triggered action exception                                   |
| 42 | Syntax error or access rule violation                     | 000 | No subclass                                                  |
| 44 | With check option violation                               | 000 | No subclass                                                  |
| HZ | Remote database access                                    | 000 | No subclass                                                  |
+----+-----------------------------------------------------------+-----+--------------------------------------------------------------+

回答by ddsultan

You can also use getErrorCode()method to handle exceptions correctly, especially useful when you work with stored procedures or functions and you have got your own custom error codes.

您还可以使用getErrorCode()method 来正确处理异常,这在您使用存储过程或函数并且拥有自己的自定义错误代码时特别有用。

It may be helpful for someone having a similar context.

这可能对有类似背景的人有所帮助。

回答by Mirwise Khan

In the catch clause you can be more specific to handle that Exception.

在 catch 子句中,您可以更具体地处理那个Exception

try {

// Your code here

} catch(SQLException ex){
    if(ex instanceof SQLIntegrityConstraintViolationException) {
          // Handle Here
    }
}