JDBC Exploration SQLException Analysis

JDBC Exploration SQLException Analysis

1. Overview of SQLException

When an error occurs when using JDBC to interact with a data source (the data source in this article represents the database we actually use), an exception named SQLException will be thrown. A SQLException contains the following information to help us better locate the error.

The error indicates:

Use the getMessage method to get it.

SQLState Code

The code consists of five letters and numbers. Most of the codes are standardized by ISO/ANSI and the Open Group (X/Open), but there are still some codes implemented by database vendors themselves.

Use the getSQLState method to obtain it.

Error Code

Unlike SQLState, error codes are integer values ​​defined by the database provider, with the possibility of actual error codes returned by the underlying data source.

Use the getErrorCode method to obtain it.

Cause

Indicates the cause of the exception. By continuously calling the getCause method, you can get the underlying cause of the exception.

Exception Chain

If multiple errors occur, the exceptions are referenced through this chain.

Use the getNextException method to get it.

2. SQLException Example

public static void printSQLException(SQLException e){
  for(Throwable e :ex){
    if (e instanceof SQLException){
      if (ignoreSQLException(((SQLException)e).getSQLState()) == false) {
        e.printStackTrace(System.err);
        System.err.println("SQLState: " + ((SQLException)e).getSQLState());
        System.err.println("Error Code: " + ((SQLException)e).getErrorCode());
        System.err.println("Message: " + e.getMessage());
        Throwable t = ex.getCause();
        while(t != null){
          System.out.println("Cause : " + t);
          t = t.getCause();
        }
      }
    }
  }
}
public static boolean ignoreSQLException(String sqlState){
  if(sqlState == null){
    System.out.println("The SQL state is not defined");
  }
  // X0Y32: Jar file already exists in schema
  if (sqlState.equalsIgnoreCase("X0Y32")) {
    return true;
  }
  // 42Y55: Table already exists in schema
  if(sqlState.equalsIgnoreCase("42Y55")){
    return true;
  }
  return true;
}

Note: The above code is taken from [http:docs.oracle.com] (http://docs.oracle.com/javase/tutorial/jdbc/basics/sqlexception.html).

3. SQLWarning

SQLWarning is a very important subclass of SQLException, used to indicate warnings that occur during database access. As an exception, a SQLWarning does not stop the execution of the application, but rather alerts the user that nothing is happening as planned. For example, a warning might notify you that an attempt to revoke a permission was unsuccessful, or inform you that an error might have occurred while requesting a disconnect.

SQLWarning may be reported by Connection, Statement (including PreparedStatement and CallableStatement) or ResultSet. These classes all have a getWarnings method. Only by calling this method can you see the first warning reported on the calling object. If getWarning returns a warning, we can call its getNextWarning method to get the next warning. Each time a line of statements is executed, the warning of the previous line of statements will be cleared, which means that if we want to retrieve the warnings from the report processing, we must retrieve it before the next line of statements is executed.

DataTruncation is the most common warning, with SQLState code 01004, indicating problems when reading or writing data. DataTruncation has many methods that help us understand which column or parameter data is truncated, whether the truncation is in the read or write operation, how many bytes should be transferred and how many bytes are actually transferred.

4. Other types of SQLException

BatchUpdateException: Thrown when an error occurs during a batch update operation, in addition to the provided message, all statements that were killed before the error occurred are killed with the provided update count.

SQLClientInfoException: Thrown when one or more client information properties cannot be set on a connection. In addition to the information provided, a list of client information properties that are not set is also provided.

so on...

Summarize

The above is all the content of this article about SQLException parsing in JDBC exploration. I hope it will be helpful to you. Interested friends can continue to refer to this site: Summary of Common JDBC Interfaces, Code Examples of Using JDBC to Implement Data Access Object Layer (DAO), etc. If you have any questions, you can leave a message at any time. The editor will reply to you in time. Everyone is welcome to leave a message for discussion.

You may also be interested in:
  • Solution to System.OutOfMemoryException in SQL Server
  • System.Data.SqlClient.SqlException: Unable to open database requested by the login The login failed.
  • java.sql.SQLException: Internal error: Unable to construct a Datum from the specified input

<<:  Use pure JS to achieve the secondary menu effect

>>:  VMware12.0 installation Ubuntu14.04 LTS tutorial

Recommend

Detailed tutorial on installing Prometheus with Docker

Table of contents 1. Install Node Exporter 2. Ins...

How to display the border when td is empty

Previously, I summarized how to use CSS to achieve...

Detailed explanation of uniapp's global variable implementation

Preface This article summarizes some implementati...

Example of using Nginx to implement port forwarding TCP proxy

Table of contents Demand Background Why use Nginx...

Solution to mysql failure to start due to insufficient disk space in ubuntu

Preface Recently, I added two fields to a table i...

Solution to leaving gaps between BootStrap grids

Table of contents [See an example]: [The original...

Docker Compose installation methods in different environments

1. Online installation Currently only tried the L...

How to operate Linux file and folder permissions

Linux file permissions First, let's check the...

10 content-related principles to improve website performance

<br />English address: http://developer.yaho...

js implements axios limit request queue

Table of contents The background is: What will ha...

Use h1, h2, and h3 tags appropriately

In the process of making web pages, it is inevita...

Why the CSS attribute value clear:right does not work in detail

Using the clear property to clear floats is a comm...