0

Im getting an sql syntax error somewhere at the end of this code line, i have tweaked it and mixed a bit with it but so far i haven't solved it.

 strSQL="INSERT INTO " + tableName + " VALUES " + "(" + id + ",'" + rname + "','" + rfn + "','" + rmn + ")";

Any help appreciated!

3
  • 3
    This is just aside your question but You should (seriously) have a look at what is SQL Injection and how to avoid it. Commented Nov 6, 2013 at 13:29
  • That why PreparedStatement is used. Commented Nov 6, 2013 at 13:31
  • Yea i know im used with doing it in php, though this is just a school assignment so im not going to bother making it sql injection safe. Commented Nov 6, 2013 at 13:32

3 Answers 3

6

You are missing a quote at the end

+ rmn + " )";
         ^---------here

But actually you should rather use Prepared Statements.

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

Comments

1

You have missed several ' signs. (not only in the end, but before and after the id)

strSQL="INSERT INTO " + tableName + " VALUES " + "('" + id + "','" + rname + "','" + rfn + "','" + rmn + "')";

Not related to the question, but strongly related to the proper creation of queries in Java:

You should avoid constructing queries with String contatenation. Instead, use PreparedStatement parameters. For example, you query would look like this:

strSQL="INSERT INTO " + tableName + " VALUES (?, ? , ? , ?)";

1 Comment

if the id is a number doesn't need the quote
0

There is no field name specified.write like this

INSERT INTO tbl_name (col1,col2) VALUES(val1, vale2);

Check url:http://dev.mysql.com/doc/refman/5.6/en/insert.html

4 Comments

i dont need to specify them in java.
@Looptech: my bad than. Thanks for bringing up.
@SureshKamrushi : if you are entering all the columns in the values , then you don't need to specify column names
@Looptech : where it says in java that you don't need to specify column names in java.

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.