Handling errors

DownloadDownload JDBC Driver

When using the Microsoft JDBC Driver for SQL Server, all database error conditions are returned to your Java application as exceptions using the SQLServerException class. The following methods of the SQLServerException class are inherited from java.sql.SQLException and java.lang.Throwable; and they can be used to return specific information about the SQL Server error that has occurred:

  • getSQLState() returns the standard X/Open or SQL99 state code of the exception.

  • getErrorCode() returns the specific database error number.

  • getMessage() returns the full text of the exception. The error message text describes the problem, and frequently includes placeholders for information, such as object names, that are inserted in the error message when it is displayed.

  • getNextException() returns the next SQLServerException object or null if there are no more exception objects to return.

  • getSQLServerError() returns the SQLServerError object containing detailed info about the exception as received from SQL Server. This method returns null if no server error has occurred.

The following methods of the SQLServerError class can be used to obtain additional details about the error generated from the server.

  • SQLServerError.getErrorMessage() returns the error message as received from the server.

  • SQLServerError.getErrorNumber() returns a number that identifies the type of the error.

  • SQLServerError.getErrorState() returns a numeric error code from SQL Server that represents an error, warning or "no data found" message.

  • SQLServerError.getErrorSeverity() returns the severity level of the error received.

  • SQLServerError.getServerName() returns the name of the computer that is running an instance of SQL Server that generated the error.

  • SQLServerError.getProcedureName() returns the name of the stored procedure or remote procedure call (RPC) that generated the error.

  • SQLServerError.getLineNumber() returns the line number within the Transact-SQL command batch or stored procedure that generated the error.

In the following example, an open connection to the SQL ServerAdventureWorks sample database is passed in to the function and a malformed SQL statement is constructed that does not have a FROM clause. Then, the statement is run and an SQL exception is processed.

public static void executeSQLException(Connection con) {
    try (Statement stmt = con.createStatement();) {
        String SQL = "SELECT TOP 10 * Person.Contact";
        ResultSet rs = stmt.executeQuery(SQL);

        while (rs.next()) {
            System.out.println(rs.getString("FirstName") + " " + rs.getString("LastName"));
        }
    }
    catch (SQLException se) {
        do {
            System.out.println("SQL STATE: " + se.getSQLState());
            System.out.println("ERROR CODE: " + se.getErrorCode());
            System.out.println("MESSAGE: " + se.getMessage());
            System.out.println();
            se = se.getNextException();
        }
        while (se != null);
    }
}

See also

Diagnosing problems with the JDBC driver