The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
This page covers the following topics:本页涵盖以下主题:
When JDBC encounters an error during an interaction with a data source, it throws an instance of 当JDBC在与数据源的交互过程中遇到错误时,它抛出一个SQLException
as opposed to Exception
. SQLException
实例,而不是Exception
实例。(A data source in this context represents the database to which a (此上下文中的数据源表示Connection
object is connected.) Connection
对象所连接的数据库。)The SQLException
instance contains the following information that can help you determine the cause of the error:SQLException
实例包含以下信息,可帮助您确定错误原因:
A description of the error. 对错误的描述。Retrieve the 通过调用方法String
object that contains this description by calling the method SQLException.getMessage
.SQLException.getMessage
来检索包含此描述的String
对象。
A SQLState code. SQLState代码。These codes and their respective meanings have been standardized by ISO/ANSI and Open Group (X/Open), although some codes have been reserved for database vendors to define for themselves. 这些代码及其各自的含义已由ISO/ANSI和Open集团(X/Open)标准化,尽管有些代码保留给数据库供应商自行定义。This 此String
object consists of five alphanumeric characters. String
对象由五个字母数字字符组成。Retrieve this code by calling the method 通过调用方法SQLException.getSQLState
.SQLException.getSQLState
检索此代码。
An error code. 错误代码。This is an integer value identifying the error that caused the 这是一个整数值,标识导致抛出SQLException
instance to be thrown. SQLException
实例的错误。Its value and meaning are implementation-specific and might be the actual error code returned by the underlying data source. 它的值和含义是特定于实现的,可能是底层数据源返回的实际错误代码。Retrieve the error by calling the method 通过调用方法SQLException.getErrorCode
.SQLException.getErrorCode
检索错误。
A cause. 原因。A SQLException
instance might have a causal relationship, which consists of one or more Throwable
objects that caused the SQLException
instance to be thrown. SQLException
实例可能具有因果关系,该因果关系由导致SQLException
实例被抛出的一个或多个Throwable
对象组成。To navigate this chain of causes, recursively call the method 要导航此原因链,请递归调用方法SQLException.getCause
until a null
value is returned.SQLException.getCause
,直到返回null
值。
A reference to any chained exceptions. 对任何链式异常的引用。If more than one error occurs, the exceptions are referenced through this chain. 如果发生多个错误,则通过此链引用异常。Retrieve these exceptions by calling the method 通过对引发的异常调用方法SQLException.getNextException
on the exception that was thrown.SQLException.getNextException
来检索这些异常。
The following method, 以下方法JDBCTutorialUtilities.printSQLException
outputs the SQLState, error code, error description, and cause (if there is one) contained in the SQLException
as well as any other exception chained to it:JDBCTutorialUtilities.printSQLException
输出SQLException
中包含的SQLState、错误代码、错误描述和原因(如果有)以及链接到它的任何其他异常:
public static void printSQLException(SQLException ex) { for (Throwable e : ex) { if (e instanceof SQLException) { if (ignoreSQLException( ((SQLException)e). getSQLState()) == false) { e.printStackTrace(System.err); System.err.println("SQLState: " + ((SQLException)e).getSQLState()); System.err.println("Error Code: " + ((SQLException)e).getErrorCode()); System.err.println("Message: " + e.getMessage()); Throwable t = ex.getCause(); while(t != null) { System.out.println("Cause: " + t); t = t.getCause(); } } } } }
For example, if you call the method 例如,如果使用Java DB作为DBMS调用方法CoffeesTable.dropTable
with Java DB as your DBMS, the table COFFEES
does not exist, and you remove the call to JDBCTutorialUtilities.ignoreSQLException
, the output will be similar to the following:CoffeesTable.dropTable
,而且表COFFEES
不存在,并且删除了对JDBCTutorialUtilities.ignoreSQLException
的调用,输出将类似于以下内容:
SQLState: 42Y55 Error Code: 30000 Message: 'DROP TABLE' cannot be performed on 'TESTDB.COFFEES' because it does not exist.
Instead of outputting 您可以先检索SQLException
information, you could instead first retrieve the SQLState
then process the SQLException
accordingly. SQLState
,然后相应地处理SQLException
,而不是输出SQLException
信息。For example, the method 例如,如果JDBCTutorialUtilities.ignoreSQLException
returns true
if the SQLState
is equal to code 42Y55
(and you are using Java DB as your DBMS), which causes JDBCTutorialUtilities.printSQLException
to ignore the SQLException
:SQLState
等于代码42Y55
(并且您正在使用Java DB作为DBMS),则方法JDBCTutorialUtilities.ignoreSQLException
返回true
,这会导致JDBCTutorialUtilities.printSQLException
忽略SQLException
:
public static boolean ignoreSQLException(String sqlState) { if (sqlState == null) { System.out.println("The SQL state is not defined!"); return false; } // X0Y32: Jar file already exists in schema if (sqlState.equalsIgnoreCase("X0Y32")) return true; // 42Y55: Table already exists in schema if (sqlState.equalsIgnoreCase("42Y55")) return true; return false; }
SQLWarning
objects are a subclass of SQLException
that deal with database access warnings. SQLWarning
对象是处理数据库访问警告的SQLException
的子类。Warnings do not stop the execution of an application, as exceptions do; they simply alert the user that something did not happen as planned. 警告不会像异常一样停止应用程序的执行;它们只是提醒用户某些事情没有按计划发生。For example, a warning might let you know that a privilege you attempted to revoke was not revoked. 例如,警告可能会让您知道您试图撤销的特权未被撤销。Or a warning might tell you that an error occurred during a requested disconnection.或者,警告可能会告诉您,在请求的断开连接过程中发生了错误。
A warning can be reported on a 可以在Connection
object, a Statement
object (including PreparedStatement
and CallableStatement
objects), or a ResultSet
object. Connection
对象、Statement
对象(包括PreparedStatement
对象和CallableStatement
对象)或ResultSet
对象上报告警告。Each of these classes has a 这些类中的每一个都有一个getWarnings
method, which you must invoke in order to see the first warning reported on the calling object. getWarnings
方法,您必须调用该方法才能看到在调用对象上报告的第一个警告。If 如果getWarnings
returns a warning, you can call the SQLWarning
method getNextWarning
on it to get any additional warnings. getWarnings
返回警告,则可以对其调用SQLWarning
方法getNextWarning
以获取任何其他警告。Executing a statement automatically clears the warnings from a previous statement, so they do not build up. 执行一条语句会自动清除上一条语句中的警告,因此它们不会累积。This means, however, that if you want to retrieve warnings reported on a statement, you must do so before you execute another statement.但是,这意味着,如果要检索语句中报告的警告,则必须在执行另一条语句之前进行检索。
The following methods from JDBCTutorialUtilities
illustrate how to get complete information about any warnings reported on Statement
or ResultSet
objects:JDBCTutorialUtilities
中的以下方法说明了如何获取有关在Statement
或ResultSet
对象上报告的任何警告的完整信息:
public static void getWarningsFromResultSet(ResultSet rs) throws SQLException { JDBCTutorialUtilities.printWarnings(rs.getWarnings()); } public static void getWarningsFromStatement(Statement stmt) throws SQLException { JDBCTutorialUtilities.printWarnings(stmt.getWarnings()); } public static void printWarnings(SQLWarning warning) throws SQLException { if (warning != null) { System.out.println("\n---Warning---\n"); while (warning != null) { System.out.println("Message: " + warning.getMessage()); System.out.println("SQLState: " + warning.getSQLState()); System.out.print("Vendor error code: "); System.out.println(warning.getErrorCode()); System.out.println(""); warning = warning.getNextWarning(); } }
The most common warning is a 最常见的警告是DataTruncation
warning, a subclass of SQLWarning
. DataTruncation
警告,它是SQLWarning
的子类。All 所有DataTruncation
objects have a SQLState of 01004
, indicating that there was a problem with reading or writing data. DataTruncation
对象的SQLState
均为01004
,这表明读取或写入数据时存在问题。DataTruncation
methods let you find out in which column or parameter data was truncated, whether the truncation was on a read or write operation, how many bytes should have been transferred, and how many bytes were actually transferred.DataTruncation
方法可以让您找出截断了哪些列或参数数据,截断是在读操作还是写操作上,应该传输多少字节,以及实际传输了多少字节。
Your JDBC driver might throw a subclass of 您的JDBC驱动程序可能会抛出SQLException
that corresponds to a common SQLState or a common error state that is not associated with a specific SQLState class value. SQLException
的子类,该子类对应于与特定SQLState类值无关的公共SQLState或公共错误状态。This enables you to write more portable error-handling code. 这使您能够编写更多可移植的错误处理代码。These exceptions are subclasses of one of the following classes:这些异常是以下类之一的子类:
SQLNonTransientException
SQLTransientException
SQLRecoverableException
See the latest Javadoc of the 有关这些子类的更多信息,请参阅java.sql
package or the documentation of your JDBC driver for more information about these subclasses.java.sql
包的最新Javadoc或JDBC驱动程序的文档。
The following subclasses of 还可以抛出SQLException
can also be thrown:SQLException
的以下子类:
BatchUpdateException
SQLException
, BatchUpdateException
provides the update counts for all statements that were executed before the error occurred.SQLException
提供的信息外,BatchUpdateException
还提供错误发生前执行的所有语句的更新计数。SQLClientInfoException
is thrown when one or more client information properties could not be set on a Connection. SQLClientInfoException
。SQLException
, SQLClientInfoException
provides a list of client information properties that were not set.SQLException
提供的信息外,SQLClientInfoException
还提供未设置的客户端信息属性列表。