I am trying to write an INSERT to Oracle DB using PreparedStatement, but I only get this error. At the moment, my efforts to overcome it far outweigh my progress, so a different set of eyes can help. Where is an invalid character?
A lot of what I found suggests that the final " ; " inside your sql string may be the culprit, but I did not have it in my statement from the very beginning.
My very connection, which works fine in several other places in the program:
Properties props = new Properties(); props.setProperty( "user", username ); props.setProperty( "password", password ); props.setProperty( "defaultRowPrefetch", "10" ); props.setProperty( "defaultBatchValue", "10" ); props.setProperty( "processEscapes", "false" ); DriverManager.registerDriver(new oracle.jdbc.OracleDriver()); Connection conn = DriverManager.getConnection(DB_URL_SVC, props);
How I would like to do this (except that I would wrap it in a method that takes three lines), but it throws an SQLSyntaxErrorException
String INSERT_BIKE = "INSERT INTO RACEBIKES ( BIKENAME , COUNTRY_OF_ORIGIN , COST ) VALUES ( ? , ? , ? )"; PreparedStatement preStatement = conn.prepareStatement( INSERT_BIKE ); preStatement.setString(1, "JHT"); preStatement.setString(2, "USA"); preStatement.setInt(3, 2500); preStatement.executeUpdate(); // ORA-00911: invalid character error
This one works , but defeats the purpose of using PreparedStatement, since the parameters are hard-coded:
String INSERT_BIKE = "INSERT INTO RACEBIKES ( BIKENAME , COUNTRY_OF_ORIGIN , COST ) VALUES ( 'JHT' , 'USA' , '2500' )"; PreparedStatement preStatement = conn.prepareStatement( INSERT_BIKE ); preStatement.executeUpdate();
Work . However, I understand that combining variables with single and double quotes is also not really the best way, since PreparedStatement should save us from having to deal with that part of the syntax:
String value1 = "JHT"; String value2 = "USA"; int value3 = 2500; String INSERT_BIKE = "INSERT INTO RACEBIKES ( BIKENAME , COUNTRY_OF_ORIGIN , COST ) VALUES ( '" + value1 + "', '" + value2 + "', '" + value3 + "' )"; PreparedStatement preStatement = conn.prepareStatement( INSERT_BIKE ); preStatement.executeUpdate();
Crash with SQLSyntaxErrorException . So even if the syntax for code quotes is by itself, I still cannot place these variables in preStatement.setString (), which at least allows a little flexibility.
String INSERT_BIKE = "INSERT INTO RACEBIKES ( BIKENAME , COUNTRY_OF_ORIGIN , COST ) VALUES ( ? , ? , ? )"; PreparedStatement preStatement = conn.prepareStatement( INSERT_BIKE ); preStatement.setString(1, "' + value1 + '"); preStatement.setString(2, "' + value2 + '"); preStatement.setInt(3, "' + value3 + '"); preStatement.executeUpdate(); // ORA-00911: invalid character error
Failed. Attaching placeholders in my single-quoted string results in a SQLException .
String INSERT_BIKE = "INSERT INTO RACEBIKES ( BIKENAME , COUNTRY_OF_ORIGIN , COST ) VALUES ( '?' , '?' , '?' )"; PreparedStatement preStatement = conn.prepareStatement( INSERT_BIKE ); preStatement.setString(1, "JHT"); preStatement.setString(2, "USA"); preStatement.setInt(3, 2500); preStatement.executeUpdate(); // invalid column index
Failure . Attaching two String (but not int ) placeholders in my single-quoted string results in a SQLException .
String INSERT_BIKE = "INSERT INTO RACEBIKES ( BIKENAME , COUNTRY_OF_ORIGIN , COST ) VALUES ( '?' , '?' , ? )"; PreparedStatement preStatement = conn.prepareStatement( INSERT_BIKE ); preStatement.setString(1, "JHT"); preStatement.setString(2, "USA"); preStatement.setInt(3, 2500); preStatement.executeUpdate(); // invalid column index
This one does not work, but does not write to the database (even if I did not turn off auto-commit).
String INSERT_BIKE = "INSERT INTO RACEBIKES ( BIKENAME , COUNTRY_OF_ORIGIN , COST ) VALUES ( ? , ? , ? )"; PreparedStatement preStatement = conn.prepareStatement( INSERT_BIKE ); preStatement.setString(1, "JHT"); preStatement.setString(2, "USA"); preStatement.setInt(3, 2500); preStatement.executeBatch();
I also tried all questions about escapes with backslashes, double backslashes, backticks, quitsies, no-startsies, erasies, double-stamp and tofus-make-it-true ! Maybe someone out there knows voodoo that will help me ?!