- 异常处理允许我们以受控的方式来处理异常情况,而不是直接退出程序,例如程序定义的错误。
- 发生异常时可以直接抛出异常。术语“异常”表示当前的程序直接停止,并且被重定向到最近的适用的catch子句中。如果没有适用的catch子句存在,则程序的执行结束。
- JDBC异常处理与Java异常处理非常相似,但是对于JDBC,要处理的最常见的异常为:
java.sql.SQLException
1、SQLException方法
- 驱动程序和数据库中都会发生SQLException。发生这种异常时,SQLException类型的对象将被传递给catch子句。
- 传递的SQLException对象具有以下可用于检索有关异常的信息方法:
方法 | 描述 |
---|---|
getErrorCode( ) | 获取与异常关联的错误代码。 |
getMessage( ) | 获取驱动程序处理的错误的JDBC驱动程序的错误消息,或获取数据库错误的Oracle错误代码和消息。 |
getSQLState( ) | 获取XOPEN SQLstate字符串。 对于JDBC驱动程序错误,不会从此方法返回有用的信息。 对于数据库错误,返回五位数的XOPEN SQLstate代码。 此方法可以返回null 。 |
getNextException( ) | 获取异常链中的下一个Exception 对象。 |
printStackTrace( ) | 打印当前异常或可抛出的异常,并将其追溯到标准错误流。 |
printStackTrace(PrintStream s) | 将此throwable 及其回溯打印到指定的打印流。 |
printStackTrace(PrintWriter w) | 打印这个throwable ,它是回溯到指定的打印器(PrintWriter )。 |
- 通过利用Exception对象提供的信息,可以捕获异常并适当地继续执行程序。下面是一个try块的一般形式:
try { // Your risky code goes between these curly braces!!! }catch(Exception ex) { // Your exception handling code goes between these // curly braces, similar to the exception clause // in a PL/SQL block. }finally { // Your must-always-be-executed code goes between these // curly braces. Like closing database connection. }
2、示例代码
- 学习研究以下示例代码以了解try …. catch … finally块的用法。
package com.geeklicreed.jdbc; import java.sql.*; public class TryCatchFinally { // JDBC driver name and database URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/EMP"; // Database credentials static final String USER = "root"; static final String PASS = "02000059"; public static void main(String[] args) { Connection conn = null; try{ //STEP 2: Register JDBC driver Class.forName("com.mysql.jdbc.Driver"); //STEP 3: Open a connection System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); //STEP 4: Execute a query System.out.println("Creating statement..."); Statement stmt = conn.createStatement(); String sql; sql = "SELECT id, first, last, age FROM Employees"; ResultSet rs = stmt.executeQuery(sql); //STEP 5: Extract data from result set while(rs.next()){ //Retrieve by column name int id = rs.getInt("id"); int age = rs.getInt("age"); String first = rs.getString("first"); String last = rs.getString("last"); //Display values System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } //STEP 6: Clean-up environment rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ //Handle errors for JDBC se.printStackTrace(); }catch(Exception e){ //Handle errors for Class.forName e.printStackTrace(); }finally{ //finally block used to close resources try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); }//end finally try }//end try System.out.println("Goodbye!"); }//end main }//end JDBCExample /* 输出结果为: Connecting to database... Creating statement... ID: 100, Age: 28, First: Max, Last: Su ID: 101, Age: 25, First: Wei, Last: Wang ID: 102, Age: 30, First: Xueyou, Last: Zhang ID: 103, Age: 28, First: Jack, Last: Ma Goodbye! */