简体   繁体   中英

How to get only syntax errors in redshift from java?

I am using Amazon Redshift as my database. When I am making a database call using Java, if there is syntax error in given query, it throws

exception":"class org.postgresql.util.PSQLException

But this exception is not only for syntax errors. I get this exception if there is a connection timed out and permissions denied for user and etc.

For Example: QUERY 1: select foo from foobar

Exception and Message are as follows: {"exception":"class org.postgresql.util.PSQLException","message":"ERROR: column \\"foo\\" does not exist in foobar"}

QUERY2: select from foobar (missed the *)

Exception: {"exception":"class org.postgresql.util.PSQLException","message":"ERROR: syntax error at or near \\"from\\"\\n Position: 80"}

How can I use the exception and find that it is because of SQL syntax? because I need to display it to the user that if he enters a query with wrong syntax.

Check the SQL State value in the exception:

try {
    // code that fails
} catch (PSQLException e) {
    if (PSQLState.SYNTAX_ERROR.getState().equals(e.getSQLState())) {
        // handle syntax error
    } else }
        // handle other error
    }
}

See the javadoc of PSQLState for full list of well-defined SQL State values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM