Java SE API

SQLException.getSQLState

Read the SQLState string if supplied. Actual database execution NOT RUN.

Formal Syntax & Structure

java syntaxjava
e.getSQLState()

Working Example

Example Snippetjava
import java.sql.SQLException;
public class Main {    static String classify(SQLException e) {        if ("23505".equals(e.getSQLState())) return "duplicate";        if ("23514".equals(e.getSQLState())) return "check violation";        return "unclassified";    }    public static void main(String[] args) {        System.out.println(classify(new SQLException("synthetic", "23505")));        System.out.println(classify(new SQLException("synthetic", "23514")));        System.out.println(classify(new SQLException("synthetic")));    }}
Java Reference