Home >Java >javaTutorial >How to handle exceptions in JDBC? Exception handling in JDBC
The content of this article is to introduce how to handle exceptions in JDBC? Exception handling in JDBC. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.
JDBC's exception handling is very similar to Java exception handling, but for JDBC, the most common exception we will handle is java.sql.SQLException, which is the basic exception class of JDBC exceptions.
We will use the java.sql.SQLException exception to introduce how JDBC handles exceptions. First, let’s take a look at the relevant knowledge of java.sql.SQLException:
SQLException method
SQLException can occur in the driver and database. When such an exception occurs, an object of type SQLException is passed to the catch clause.
The passed SQLException object has the following methods that can be used to retrieve additional information about the exception:
Exception Handling for JDBC
By utilizing the information provided by the Exception object, you can catch the exception and continue your program appropriately. This is the general form of try block:
try { // 你的危险代码在这些大括号之间!!!! }catch(Exception ex) { // 你的异常处理代码介于这些代码之间。 }finally { // 必须始终执行这些代码 }
Example:
Study the following example code to understand the usage of try....catch...finally block.
//步骤1:导入必需的软件包 import java.sql.*; public class JDBCExample { // JDBC 驱动程序名称和数据库URL static final String JDBC_DRIVER = "com.mysql.jdbc.Driver"; static final String DB_URL = "jdbc:mysql://localhost/EMP"; // 数据库的凭证 static final String USER = "username"; static final String PASS = "password"; public static void main(String[] args) { Connection conn = null; try{ //步骤 2: 注册JDBC驱动程序 Class.forName("com.mysql.jdbc.Driver"); //步骤 3: 打开连接 System.out.println("Connecting to database..."); conn = DriverManager.getConnection(DB_URL,USER,PASS); //步骤 4: 执行查询 System.out.println("Creating statement..."); Statement stmt = conn.createStatement(); String sql; sql = "SELECT id, first, last, age FROM Employees"; ResultSet rs = stmt.executeQuery(sql); //步骤 5: 从结果集中提取数据 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"); //显示值 System.out.print("ID: " + id); System.out.print(", Age: " + age); System.out.print(", First: " + first); System.out.println(", Last: " + last); } //步骤 6: Clean-up environment rs.close(); stmt.close(); conn.close(); }catch(SQLException se){ //处理JDBC错误 se.printStackTrace(); }catch(Exception e){ //处理Class.forName的错误 e.printStackTrace(); }finally{ //使用finally块,关闭连接 try{ if(conn!=null) conn.close(); }catch(SQLException se){ se.printStackTrace(); }//end finally try }//end try System.out.println("Goodbye!"); }//end main }//end JDBCExample
Now, let us compile the above example as follows:
When running JDBCExample, if there is no problem, it will produce the following results, otherwise it will capture Corresponding error and display error message
Try the above example by passing wrong database name or wrong username or password and check the result.
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
The above is the detailed content of How to handle exceptions in JDBC? Exception handling in JDBC. For more information, please follow other related articles on the PHP Chinese website!