Wednesday, March 9, 2011

java.sql.SQLException: ORA-00911: invalid character

Tags: Java, JDBC, Oracle, ORA-00911, invalid character, semicolon

Here you have the formal description of this fault:


ORA-00911:

invalid character
Cause:identifiers may not start with any ASCII character other than letters and numbers. $#_ are also allowed after the first character. Identifiers enclosed by doublequotes may contain any character other than a doublequote. Alternative quotes (q'#...#') cannot use spaces, tabs, or carriage returns as delimiters. For all other contexts, consult the SQL Language Reference Manual.
Action:none


... but it doesn't help much if your SQL query looks fine and runs well in Toad or PL SQL Developer.

When I got this exception I thought I could have add some strange non-printable character in my StringBuilder object, but it was easier:

JDBC queries are not allowed to have semicolons.

If you have this issue, check if you have a semicolon ";" at the end of the query.

// this will work fine in Toad or PL SQL Developer but will raise ORA-00911
ResultSet result = st.executeQuery("select * from dual ; ");


// this will work fine in Java, JDBC
ResultSet result = st.executeQuery("select * from dual");



Reference:
http://ora-00911.ora-code.com/
http://www.fromdev.com/2008/09/javasqlsqlexception-ora-00911-invalid.html