Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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")";
like image 271
user1080390 Avatar asked Feb 20 '26 00:02

user1080390


2 Answers

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')";
like image 100
Jeremy Avatar answered Feb 21 '26 14:02

Jeremy


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')";
like image 26
Ernest Friedman-Hill Avatar answered Feb 21 '26 14:02

Ernest Friedman-Hill



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!