0

I have tried several ideas using prepared statement, but none of them worked. These are:

String insertSQL = "INSERT INTO APP.TEST (Name, 'role') VALUES (?, ?)";

String insertSQL = "INSERT INTO APP.TEST (Name, [role]) VALUES (?, ?)";

String insertSQL = "INSERT INTO APP.TEST (Name, role) VALUES (?, ?)";
14
  • What do you mean "none of them worked"? What happened? Commented Apr 5, 2015 at 16:23
  • use role , it should work. It seems overstackflow remove the chars. Commented Apr 5, 2015 at 16:24
  • I tried that as well Commented Apr 5, 2015 at 16:25
  • Derby docs specify double quotes for identifiers. db.apache.org/derby/docs/10.7/ref/crefsqlj1003454.html Commented Apr 5, 2015 at 16:25
  • But role is not on the list of reserved words Commented Apr 5, 2015 at 16:27

1 Answer 1

2

Apache Derby's documentation specifies that delimited SQL identifiers may be double-quoted. So your SQL string would eventually look like:

INSERT INTO APP.TEST (Name, "role") VALUES (?, ?)

Since that is being placed inside a string already double-quoted, backslash-escape the inner quotes:

String insertSQL = "INSERT INTO APP.TEST (Name, \"role\") VALUES (?, ?)";

Derby's list of reserved words does not currently specify role as reserved, but if in your implementation it does appear to be reserved, you must quote it accordingly.

Sign up to request clarification or add additional context in comments.

2 Comments

I encountered another issue, which is SQL name conflict with the table name. I tried String insertSQL = "INSERT INTO DATABASE.\"User\" (Name, username, password, \"role\") " + "VALUES (?, ?, ?, ?)"; but it did not work.
Quote both parts I suppose, like \"DATABASE\".\"User\" -- that's how the documentation example looks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.