OceanBase
English日本語简体中文
Docs Home
Products
Integrations
Release Notes

Databases

OceanBase DatabaseOceanBase Cloud OceanBase Best Practices

Tools

OceanBase Cloud PlatformOceanBase Migration ServiceOceanBase Developer CenterOceanBase Migration AssessmentOceanBase Admin ToolOceanBase Loader and DumperOceanBase DeployerKubernetes operator for OceanBaseOceanBase Diagnostic Tool OceanBase Binlog Service

Connectors and Middleware

OceanBase Database Proxy OceanBase Connector/C OceanBase Connector/J OceanBase Connector/ODBC OceanBase Connector/NETOceanBase Command-Line Client

Databases

OceanBase DatabaseOceanBase Cloud OceanBase Best Practices

Connectors and Middleware

OceanBase Database Proxy OceanBase Connector/C OceanBase Connector/J OceanBase Connector/ODBC OceanBase Connector/NETOceanBase Command-Line Client

Tools

OceanBase Cloud PlatformOceanBase Migration ServiceOceanBase Developer CenterOceanBase Migration AssessmentOceanBase Admin ToolOceanBase Loader and DumperOceanBase DeployerKubernetes operator for OceanBaseOceanBase Diagnostic Tool OceanBase Binlog Service
OceanBase DatabaseOceanBase Cloud ToolsConnectors and MiddlewareLifecycle Support Policy

OceanBase Connector/J

V2.2.0
  • What is OceanBase Connector J?
    • JDBC overview
    • Compatibility requirements
  • Instructions
    • Install OceanBase Client
    • Basic operations
      • Import packages
      • Create a database connection
      • Create a Statement object
      • Run queries and retrieve ResultSet objects
      • Process a ResultSet object
      • Close ResultSet and Statement objects
      • Modify a database
      • Commit changes
      • Close a database connection
    • Handle SQL exceptions
    • Java data streams
      • Overview of Java data streams
      • LONG and LONG RAW data types
      • LOB data types
    • Use LOBs
      • Overview of LOB data types
      • LOB data API
      • LOB locator API
      • CHAR, VARCHAR, and RAW data types
    • Use CallableStatements to call stored procedures
    • Data sources and URL
      • Data source overview
      • Data source characteristics and properties
      • Create and connect to a data source instance
      • Database URL
    • Result set
      • Limits and degradation rules
      • Fetch size
      • Refetch rows
      • Cursor result sets
    • Statement caching
      • Statement caching overview
      • Use statement cache
      • Reuse statement objects
    • Failover and load balancing modes
    • Batch processing
    • JDBC security features
    • Troubleshooting
    • Common APIs
      • java.sql.Connection
      • java.sql.CallableStatement
      • java.sql.DatabaseMetaData
      • java.sql.Driver
      • java.sql.PreparedStatement
      • java.sql.ParameterMetaData
      • java.sql.ResultSet
      • java.sql.ResultSetMetaData
      • java.sql.Statement
      • javax.sql.ConnectionPoolDataSource
      • javax.naming.Context
      • javax.sql.PooledConnection
    • Reference
      • Supported SQL and PL data types
  • Features specific to the Oracle mode
    • Call PL stored procedures
    • Result set performance
    • Create a multi-level collection type
    • Use ARRAY and STRUCT classes
    • Distributed transactions
      • Overview of distributed transactions
      • XA components
      • Distributed transaction examples
    • Error messages in Oracle mode
    • Data types supported in Oracle mode
  • Release Notes
    • V2.2
      • OceanBase Connector/J V2.2.0

Download PDF

Document Feedback
  1. Documentation Center
  2. OceanBase Connector/J
  3. V2.2.0

Handle SQL exceptions

Last Updated:2026-09-04 08:24:25

Retrieve error information
Display the stack trace

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:

  • getMessage

  • getErrorCode

  • getSQLState

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)

Previous topic

Close a database connection
Last

Next topic

Overview of Java data streams
Next
on this page
Retrieve error information
Display the stack trace