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

Examples of preview functions for various types of files in vue3

Table of contents Preface 1. Preview of office do...

Vue.set() and this.$set() usage and difference

When we use Vue for development, we may encounter...

Detailed process of changing apt source to Alibaba Cloud source in Ubuntu 18.04

Table of contents Preface: Ubuntu 18.04 changes a...

How to modify mysql to allow remote connections

Regarding the issue of MySQL remote connection, w...

How to solve the mysql ERROR 1045 (28000)-- Access denied for user problem

Problem description (the following discussion is ...

Solution to Linux server graphics card crash

When the resolution of the login interface is par...

Three ways to achieve background blur in CSS3 (summary)

1. Normal background blur Code: <Style> htm...

Solution to nginx hiding version number and WEB server information

Nginx can not only hide version information, but ...

React mouse multi-selection function configuration method

Generally, lists have selection functions, and si...

How to set background blur with CSS

When making some pages, in order to make the page...

MySQL 5.7.30 Installation and Upgrade Issues Detailed Tutorial

wedge Because the MySQL version installed on the ...

The practical process of login status management in the vuex project

Table of contents tool: Login scenario: practice:...

Detailed explanation of HTML form elements (Part 1)

HTML forms are used to collect different types of...