When an exception occurs, the OceanBase JDBC driver (OceanBase Client) throws an SQL exception and generates an instance of the java.sql.SQLException class or its subclass to handle the exception.
The exception may originate from the JDBC driver or the database. The execution result describes the exception and identifies the method that caused the exception, and can also contain other running information.
The SQLException class supports the following basic exception handling operations: retrieving error messages, error codes, and SQL status, as well as printing the stack trace.
Retrieve error information
The SQLException class provides the following methods for retrieving basic error information:
getMessagegetErrorCodegetSQLState
Example: Calling the getMessage method to display the output
catch(SQLException ecp)
{
System.out.println("exception: " + ecp.getMessage());
}
// Print exceptions caused by the JDBC driver.
exception: Invalid column type
Display the stack trace
The SQLException class provides the printStackTrace() method for displaying the stack trace. This method displays the stack trace of the thrown object based on the standard error stream. You can also specify a java.io.PrintStream or java.io.PrintWriter object to print the exception.
Example: Catching an SQL exception and printing the stack trace.
// SQL code example: Traverse the results and print the employee names.
try {
while (rs.next ())
System.out.println (rs.getString (5)); // Assume that an invalid column index is used.
}
catch(SQLException ecp) { ecp.printStackTrace (); }
// Catch the SQL exception and print the stack trace.
try { <some code> }
catch(SQLException ecp) { ecp.printStackTrace (); }
// The program generates the following error text:
java.sql.SQLException: Invalid column index
at OceanBase.jdbc.driver.OceanBaseResultSetImpl.getDate(OceanBaseResultSetImpl.java:1556)
at emp.main(emp.java:41)