简体   繁体   中英

What's wrong with my JDBC sql statement

I'm writing a java socket app that allows a client to communicate with a server, one of the other requirements is that it also needs to initialize JDBC. I believe I have wrote my JDBC connection method correctly, and my insert statement has worked like this on similar projects. It might be a simple mistake as i'm not using an IDE, can someone tell me what is wrong with my SQL statement? All the info is right, but it won't compile.

Error:

C:\Users\imallin\My Documents> javac provider.java
Provider.java:88 ';' expected 
String sql = "Insert INTO 'users' ('ID', 'firstName') VALUES ("123","123")";

Your immediate problem is that you need to escape the double quotes that are in your string. This is because when the compiler see's another " it thinks it is the end of the String definition and exepcts a semi-colon.

String sql = "Insert INTO 'users' ('ID', 'firstName') VALUES (\"123\",\"123\")";

Now that the Java compiler is happy, you will have SQL-related issues.

In general with SQL, you will want to use single quotes to represent a string. It appears MySQL specifically allows double quotes, but only when the SQL QUOTES ANSI mode is not set . So it is best to use single quotes to represent strings here.

Here is what you probably want, assuming that the ID column is an integer, and that the firstName column is a string/varchar.

String sql = "Insert INTO users (ID, firstName) VALUES (123,'123')";

To slightly differ from the other answers that have been posted, you need to not use double quotes in your SQL. The single quotes you've used are all in the wrong places, and the double quotes are simply not allowed. Your statement should look like

String sql = "Insert INTO users (ID, firstName) VALUES ('123','123')";

It looks like you haven't escaped the double quotes in your SQL statement. Java sees your string as finishing before the first 123.

In the line:

String sql = "Insert INTO 'users' ('ID', 'firstName') VALUES ("123","123")";

The double quoted string ends after VALUES ( , and is immediately followed by a numeric token. That's illegal in Java. The immediate fix is to add backslashes:

String sql = "Insert INTO 'users' ('ID', 'firstName') VALUES (\"123\",\"123\")";

Though this would also work (assuming it's talking about integers, not strings):

String sql = "Insert INTO 'users' ('ID', 'firstName') VALUES (" + 123 + "," + 123 + ")";

More generally though, what's wrong with it is that you're doing an INSERT without using parameterization. This is virtually always the wrong thing in real code! JDBC has good support for parameterized queries, which you should use.

You can use single quotes instead.

"Insert INTO users (ID, firstName) VALUES ('123','123')";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM