Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

when i am executing my code,i code the error
Quote:
com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'null,'What 's the first day of the week ?','Date')' at line 1

and my code is:-
SQL
String sql= "INSERT INTO assign_table"+"VALUES(null,'"+question+"','"+node+"')";

why is it so?
Posted
Updated 2-Feb-15 23:53pm
v2
Comments
Richard MacCutchan 3-Feb-15 5:56am    
What is the result of all those string concatenations? Obviously not a valid piece of MySQL. And you really should not build SQL statements in that way, you should use proper parameterised queries.

It would have saved you a lot of trouble by using prepared statement, e.g
String sql = " insert into assign_table values (?, ?, ?)";
// create the mysql insert preparedstatement
PreparedStatement preparedStmt = conn.prepareStatement(sql);
preparedStmt.setString (1, null);
preparedStmt.setString (2, question);
preparedStmt.setString (3, node);

Read more A Java MySQL INSERT example (using PreparedStatement)[^]
I just noticed that your second parameter
'What 's the first day of the week ?'

contains a single quote, it has to be escaped, like this
'What \'s the first day of the week ?'

Read more: http://dev.mysql.com/doc/refman/4.1/en/string-literals.html[^]
 
Share this answer
 
v2
Comments
CHill60 3-Feb-15 7:37am    
Good spot! And good solution. 5'd
Peter Leow 3-Feb-15 7:57am    
Thank you, CHill60.
If you examine the contents of the String sql you will see that there is no space between assign_table and VALUES. There is also no need for that plus symbol. Try
Java
String sql= "INSERT INTO assign_table VALUES(null,'"+question+"','"+node+"')";
 
Share this answer
 
Comments
jeenatmathew 3-Feb-15 6:07am    
Still it is showing error
CHill60 3-Feb-15 6:13am    
try NULL instead - I can't remember if MySQL is case-sensitive (just going to try to find out as well)
CHill60 3-Feb-15 6:14am    
... that first column *is* nullable isn't it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900