IBM Data Server Driver for JDBC and SQLJ 下的异常和警告

在 JDBC 应用程序中,SQL错误会产生异常,您可以使用 try/catch 块来处理。 SQL 警告不会抛出异常,因此您需要调用方法来检查在执行 SQL 语句之后是否发生了警告。

IBM® Data Server Driver for JDBC and SQLJ 提供了以下类和接口,用于提供错误和警告信息。

SQLException

用于处理错误的 SQLException 类。 当方法执行期间发生错误时,所有 JDBC 方法都会抛出 SQLException 实例。 根据 JDBC 规范,SQLException 对象包含以下信息:
  • 包含错误代码的 int 值。 SQLException.getErrorCode 检索此值。
  • 包含 SQLSTATE 或 NULL 的 String 对象。 SQLException.getSQLState 检索此值。
  • 包含错误描述或 NULL 的 String 对象。 SQLException.getMessage 检索此值。
  • 指向下一 SQLException 或 NULL 的指针。 SQLException.getNextException 检索此值。

当 JDBC 方法抛出一个单一的 SQLException 时,该 SQLException 可能是由处理该方法时发生的潜在Java异常引起的。 IBM Data Server Driver for JDBC and SQLJ 处理该方法时。 在这种情况下,SQLException 会包装底层异常,您可以使用 SQLException.getCause 方法来检索有关错误的信息。

DB2Diagnosable

IBM Data Server Driver for JDBC and SQLJ 专用接口 com.ibm.db2.jcc.DB2Diagnosable 扩展 SQLException 班级。 DB2Diagnosable 界面提供了有关访问数据源时发生的错误的更多信息。 如果 JDBC 驱动程序检测到错误,那么 DB2Diagnosable 将提供与标准 SQLException 类相同的信息。 但是,如果数据库服务器检测到错误,那么 DB2Diagnosable 会添加以下方法,这会为您提供有关错误的其他信息:
getSqlca
返回具有以下信息的 DB2Sqlca 对象:
  • SQL 错误代码
  • SQLERRMC 值
  • SQLERRP 值
  • SQLERRD 值
  • SQLWARN 值
  • SQLSTATE
getThrowable
返回导致 SQLExceptionjava.lang.Throwable 对象,如果不存在,则返回null。
printTrace
显示诊断信息。

SQLException 子类

如果您正在使用 JDBC 4.0 或更高版本,那么可以通过捕获以下异常类来获取比 SQLException 提供的更多特定信息:
  • SQLNonTransientException

    如果在重试操作之前失败的 SQL 操作无法成功,那么将抛出 SQLNonTransientException,除非采取了一些纠正操作。 SQLNonTransientException 类具有以下子类:

    • SQLFeatureNotSupportedException
    • SQLNonTransientConnectionException
    • SQLDataException
    • SQLIntegrityConstraintViolationException
    • SQLInvalidAuthorizationSpecException
    • SQLSyntaxException
  • SQLTransientException

    当SQL操作在重试时可能成功,而无需应用程序干预时,会抛出一个 SQLTransientException 。 在 SQLTransientException 被丢弃后,连接仍然有效。 SQLTransientException 类有以下子类:

    • SQLTransientConnectionException
    • SQLTransientRollbackException
    • SQLTimeoutException
  • SQLRecoverableException

    如果应用程序执行某些恢复步骤并重试该事务,那么先前失败的操作在可能成功时会抛出 SQLRecoverableException。 在抛出 SQLRecoverableException 之后,连接不再有效。

  • SQLClientInfoException

    当无法设置一个或多个客户端属性时, Connection.setClientInfo 方法会抛出一个 SQLClientInfoExceptionSQLClientInfoException 指示无法设置的属性。

BatchUpdateException

BatchUpdateException 对象包含有关在执行批处理 SQL 语句期间发生的错误的以下项:

  • 一个 String 对象,其中包含错误描述,或者 null
  • 一个 String 对象,其中包含失败SQL语句的SQLSTATE,或者 null
  • 包含错误代码或零的整数值
  • 批处理中SQL语句的更新计数整数数组,或 null
  • 指向 SQLException 对象的指针,或 null
将为整个批处理抛出一个 BatchUpdateException。 至少一个 SQLException 对象链接到 BatchUpdateException 对象。 SQLException 对象的链接顺序与添加到批处理的相应语句的顺序相同。 为了帮助您将 SQLException 对象与批处理中的语句匹配,每个 SQLException 对象的错误描述字段以该字符串开头:
Error for batch element #n:
n 是批处理中的语句数。

批处理执行期间的 SQL 警告不会抛出 BatchUpdateException。 要获取警告信息,请使用 executeBatch 方法,在您运行 方法的对象上使用 Statement.getWarnings 方法。 然后,您可以检索每个 SQLWarning 对象的错误描述、SQLSTATE 和错误代码。

SQLWarning

当SQL语句返回正SQLCODE时,以及当SQL语句返回0 SQLCODE且SQLSTATEs不为0时,会累积警告。 IBM Data Server Driver for JDBC and SQLJ 当SQL语句返回正SQLCODE时,以及当SQL语句返回0 SQLCODE且SQLSTATEN不为0时,会累积警告。

调用 getWarnings 会检索 SQLWarning 对象。

重要提示: 当对 Statement.executeUpdatePreparedStatement.executeUpdate 的调用没有影响任何行时, IBM Data Server Driver for JDBC and SQLJ 生成一个错误代码为+100的 SQLWarning

ResultSet.next 没有返回任何记录时, IBM Data Server Driver for JDBC and SQLJ 不会生成 SQLWarning

通用 SQLWarning 对象包含以下信息:

  • 包含警告或 NULL 的描述的 String 对象
  • 包含 SQLSTATE 或 NULL 的 String 对象
  • 包含错误代码的 int
  • 指向下一个 SQLWarning 或 NULL 的指针

IBM Data Server Driver for JDBC and SQLJ ,就像一个 SQLException 对象一样,一个 SQLWarning 对象也可以包含特定于 Db2。 特定于 Db2 信息,例如 SQLWarning 对象,与 SQLException 目的。