To expand on accepted answer I'll provide JDBC code necessary to retrieve error position from SQL query.
PL/SQL:
This PL/SQL block accepts sql query text and returns error position:
DECLARE
c INTEGER := DBMS_SQL.open_cursor();
errorpos integer := -1;
BEGIN
BEGIN
DBMS_SQL.parse(c, :sqltext, DBMS_SQL.native);
EXCEPTION
WHEN OTHERS THEN
errorpos := DBMS_SQL.LAST_ERROR_POSITION();
END;
:errorpos := errorpos;
DBMS_SQL.close_cursor(c);
END;
It accepts two parameters sqltext and errorpos.
Java:
On Java side we need to call PL/SQL code from above and retrieve error position. Here's a method for that:
private int retrieveErrorPosition(Connection connection, String query) {
CallableStatement callStatement = null;
try {
callStatement = connection.prepareCall(LAST_ERROR_POSITION_QUERY);
callStatement.setString(1, query);
callStatement.registerOutParameter(2, OracleTypes.INTEGER);
callStatement.execute();
return callStatement.getInt(2);
} catch (SQLException ex) {
log.log(Level.SEVERE, "", ex);
} finally {
if (callStatement != null) {
try {
callStatement.close();
} catch (SQLException sqle) {
}
}
}
return -1;
}
Usage:
Now, if query string executes with exception we can extract error position:
String query;
Connection connection;
try {
//normal query execution
} catch (SQLException ex) {
int sqlErrorPosition = retrieveErrorPosition(connection, query);
//exception handling
}