Q:

Catching SQL errors in JAVA?

I'm getting SQL result sets in Java from my Oracle DB and I want to know if and how I can record/catch SQL errors in Java. So when I try to execute some SQL:
ResultSet resultSet = connection.executeQuery(query);

I get an error like ORA-00942: table or view does not exist. How can I record that? 

A:

Use the catch statement to store the proper SQL exception as you want/need:
try (ResultSet rs = statement.executeQuery(query)) {
    /* retrieve the data */
} catch (SQLException e) {
    /* handle the exception properly */
    storeExceptionSomewhereElse(e);
}

//...

public void storeExceptionSomewhereElse(SQLException e) {
    /*
        Here you can store the exception in database
        or external data source.
    */
}

