0
String l="UPDATE counselor SET firstName="qwe" WHERE id=1";

Could anyone tell me what's wrong with above statement,as q sql statement. Netbeans displays as "; expected". I can't understand why it isn't correct at least as a string. Code is as follows.

String l="UPDATE counselor SET firstName="qwe" WHERE id=1";
Statement m=conn.createStatement();
ResultSet v=m.executeQuery(l);
1
  • It isn't correct because your string contains more double-quotes and Java doesn't know that they're meant to be part of the value and not the end of the string. As darijan points out you need single quotes for strings in SQL too - but that's the not the Java problem. Commented Jun 23, 2013 at 10:05

2 Answers 2

3

In SQL, you should escape strings with '. So,

String l="UPDATE counselor SET firstName='qwe' WHERE id=1";
Sign up to request clarification or add additional context in comments.

4 Comments

"In SQL, you must escape strings with '" - not true.
No, you haven't changed the meaning.
@KarolyHorvath, many (most?) SQL database systems, use " to escape identifiers and ' to escape strings: ERROR: column "qwe" does not exist; SQL Status:42703. MySQL is an exception as it accepts " for strings. I suggest not to use this MySQL quirk in order to be able to support other database systems in the future more easily.
I'm aware of that, but what he wrote simply wasn't true now, was it?
0
String l="UPDATE counselor SET firstName=\"qwe\" WHERE id=1";

If you want to put quotes within quotes you must use the escape sequence, \", on the interior quotes. To print the sentence. For example

She said "Hello!" to me.

you would write

System.out.println("She said \"Hello!\" to me.");

1 Comment

I did not downvote you, but I suggest not to use this MySQL quirk. The '-character is supported by all common database system to escape strings. But using the "-character to escape strings, is a MySQL quirk.

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.