如何在JDBC中捕获特定的异常? [英] How to catch a specific exception in JDBC?

查看:617
本文介绍了如何在JDBC中捕获特定的异常?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

SQLException 包含与异常相关的一些特定于数据库的信息。来自doc:

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


每个SQLException提供几种
种信息:

Each SQLException provides several kinds of information:

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

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

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

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)
整数错误代码,特定于每个供应商
。通常这将是
底层数据库返回的
实际错误代码。

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)链接到下一个
异常。这可用于提供
其他错误信息。

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

这篇关于如何在JDBC中捕获特定的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆