在这篇文章中,我们将讨论如何处理在与数据源交互时遇到的SQLExceptions
。
当JDBC在与数据源的交互中遇到错误时,它会抛出一个SQLException
的实例,而不是Exception。(在这种情况下,数据源代表连接对象所连接的数据库)。SQLException
实例包含以下信息,可以帮助你确定错误的原因。
SQLException.getNextException
来检索这些异常。让我们写一个通用的方法来检索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();
}
}
}
}
}
例如,如果我们的SQL语句有一个语法错误,那么上述方法会打印出以下结果:
SQLState: 42000
Error Code: 1064
Message: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'where id = 3' at line 1
我们可以不输出SQLException信息,而是先检索SQLState,然后再相应地处理SQLException。例如,下面的ignoreSQLException()
方法在SQLState等于代码42Y55时返回true(并且你使用Java DB作为你的DBMS),这使得上面的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对象是SQLException
的一个子类,处理数据库访问警告。警告不会像异常那样停止应用程序的执行;它们只是提醒用户某些事情没有按计划发生。例如,一个警告可能让你知道你试图撤销的特权没有被撤销。或者一个警告可能会告诉你,在请求断开连接时发生了错误。
下面的方法说明了如何获得关于Statement或ResultSet对象上报告的任何警告的完整信息。
public static void getWarningsFromResultSet(ResultSet rs)
throws SQLException {
printWarnings(rs.getWarnings());
}
public static void getWarningsFromStatement(Statement stmt)
throws SQLException {
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();
}
}
}
最常见的警告是DataTruncation
警告,是SQLWarning
的子类。所有的DataTruncation对象的SQLState都是01004,表明在读或写数据时出现了问题。DataTruncation
方法可以让你发现在哪一列或参数中的数据被截断,截断是在读或写操作中,有多少字节应该被传输,以及有多少字节实际被传输。
你的JDBC驱动程序可能会抛出一个SQLException的子类,它对应于一个普通的SQLState或一个普通的错误状态,而这个错误状态并不与特定的SQLState类值相关。这使你能够写出更多可移植的错误处理代码。这些异常是以下类之一的子类。
以下是SQLException的子类,也可以抛出。
BatchUpdateException
是在批量更新操作中发生错误时抛出的。除了SQLException提供的信息外,BatchUpdateException还提供在错误发生前执行的所有语句的更新计数。SQLClientInfoException
是在一个或多个客户信息属性无法在连接上设置时抛出的。除了SQLException提供的信息外,SQLClientInfoException还提供了一个没有设置的客户信息属性的列表。版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.javaguides.net/2018/10/jdbc-handling-sqlexceptions.html
内容来源于网络,如有侵权,请联系作者删除!