try { // SQL code } catch(SQLException e) { System.out.println("SQL State: " + e.getSQLState()); }
import java.sql.SQLException; import java.sql.Connection; import java.sql.DriverManager; import java.sql.Statement; public class Example { public static void main(String[] args) { try { Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", ""); Statement stmt = conn.createStatement(); stmt.executeUpdate("INSERT INTO table_name VALUES (1, 'example')"); } catch(SQLException e) { System.out.println("SQL State: " + e.getSQLState()); } } }In this example, we are trying to insert values into a table named "table_name". If an SQLException is thrown, we catch it and use getSQLState() to print out the SQL State code for the exception. The package library for the java SQLException class is java.sql.