When an exception occurs, OceanBase Connector/J 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 OceanBase Connector/J or the database. The error information 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: Call the getMessage method to display the output.
catch(SQLException ecp)
{
System.out.println("exception: " + ecp.getMessage());
}
The output for an error originated from OceanBase Connector/J is usually as follows:
exception: Invalid column type
Print the stack trace
The SQLException class provides the printStackTrace() method for printing the stack trace. This method prints the stack trace of the throwable 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: Catch an SQL exception and print 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 information:
java.sql.SQLException: Invalid column index
at OceanBase.jdbc.driver.OceanBaseResultSetImpl.getDate(OceanBaseResultSetImpl.java:1556)
at emp.main(emp.java:41)