Java Mysql 简明教程
Java & MySQL - Exceptions Handling
异常处理允许您以受控方式处理异常情况(例如程序定义的错误)。
Exception handling allows you to handle exceptional conditions such as program-defined errors in a controlled fashion.
当发生异常情况时,会引发异常。术语引发表示当前程序执行停止,并且控制重定向到最近适用的 catch 子句。如果没有适用的 catch 子句,则程序执行结束。
When an exception condition occurs, an exception is thrown. The term thrown means that current program execution stops, and the control is redirected to the nearest applicable catch clause. If no applicable catch clause exists, then the program’s execution ends.
JDBC 异常处理与 Java 异常处理非常相似,但对于 JDBC,您将要处理的最常见的异常是 java.sql.SQLException.
JDBC Exception handling is very similar to the Java Exception handling but for JDBC, the most common exception you’ll deal with is java.sql.SQLException.
SQLException Methods
SQLException 可以在驱动程序和数据库中同时发生。当发生此类异常时,SQLException 类型的对象将传递给 catch 子句。
An SQLException can occur both in the driver and the database. When such an exception occurs, an object of type SQLException will be passed to the catch clause.
传递的 SQLException 对象具有以下方法,可用于检索有关异常的其他信息 −
The passed SQLException object has the following methods available for retrieving additional information about the exception −
Method |
Description |
getErrorCode( ) |
Gets the error number associated with the exception. |
getMessage( ) |
Gets the JDBC driver’s error message for an error, handled by the driver or gets the Oracle error number and message for a database error. |
getSQLState( ) |
Gets the XOPEN SQLstate string. For a JDBC driver error, no useful information is returned from this method. For a database error, the five-digit XOPEN SQLstate code is returned. This method can return null. |
getNextException( ) |
Gets the next Exception object in the exception chain. |
printStackTrace( ) |
Prints the current exception, or throwable, and it’s backtrace to a standard error stream. |
printStackTrace(PrintStream s) |
Prints this throwable and its backtrace to the print stream you specify. |
printStackTrace(PrintWriter w) |
Prints this throwable and it’s backtrace to the print writer you specify. |
通过利用异常对象中可用的信息,您可以捕获异常并适当地继续您的程序。以下是 try 块的一般形式 −
By utilizing the information available from the Exception object, you can catch an exception and continue your program appropriately. Here is the general form of a try block −
try {
// Your risky code goes between these curly braces!!!
}
catch(Exception ex) {
// Your exception handling code goes between these
// curly braces
}
finally {
// Your must-always-be-executed code goes between these
// curly braces. Like closing database connection.
}
Example
研究以下示例代码以了解 try….catch…finally 块的用法。
Study the following example code to understand the usage of try….catch…finally blocks.
这段代码是根据前一章完成的环境和数据库设置编写的。
This code has been written based on the environment and database setup done in the previous chapter.
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class TestApplication {
static final String DB_URL = "jdbc:mysql://localhost/TUTORIALSPOINT";
static final String USER = "guest";
static final String PASS = "guest123";
static final String QUERY = "{call getEmpName (?, ?)}";
public static void main(String[] args) {
// Open a connection
try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
CallableStatement stmt = conn.prepareCall(QUERY);
) {
// Bind values into the parameters.
stmt.setInt(1, 1); // This would set ID
// Because second parameter is OUT so register it
stmt.registerOutParameter(2, java.sql.Types.VARCHAR);
//Use execute method to run stored procedure.
System.out.println("Executing stored procedure..." );
stmt.execute();
//Retrieve employee name with getXXX method
String empName = stmt.getString(2);
System.out.println("Emp Name with ID: 1 is " + empName);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
现在让我们如下编译上述示例:
Now let us compile the above example as follows −
C:\>javac TestApplication.java
C:\>
当您运行 TestApplication 时,如果没有问题,它会产生以下结果,否则会捕获相应的错误并显示错误消息 −
When you run TestApplication, it produces the following result if there is no problem, otherwise the corresponding error would be caught and error message would be displayed −
C:\>java TestApplication
Executing stored procedure...
Emp Name with ID: 1 is Zara
C:\>