1

I was trying to add details to table but it showing some SQL error

Query is:

t=st1.executeUpdate("insert into stdetails(regno,nam,cid,gender,HouseName,place,guardian,phone,photo,did,Emailid,sem) values("+  reg+",'"+ n +"',"+ c +",'"+g+"','"+ h+"','"+p+"','"+ guar +"','"+ph+"','"+pic+"',"+d+",'"+e+"',"+s+"");

Error is

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 '' at line 1

3
  • 1
    Looks like one of your values is blank and you miss the single qoutes. BTW: learn about prepared Statements, to prevent SQL injection Commented Mar 8, 2017 at 7:55
  • But it is Also showing same error, when i use prepared stmnts Commented Mar 8, 2017 at 8:03
  • Please add the prepare stement to your question not as a comment Commented Mar 8, 2017 at 9:29

2 Answers 2

1

Your are missing the parentheses ) in the end of your query so it should look like :

t = st1.executeUpdate("...." + s + ")");
//----------------------------------^---

BUT

Instead of using this way, this can cause a syntax error like your case, and can cause an Sql Injection you have to use PreparedStatement.

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

1 Comment

you are welcome, instead, i suggest to use PrepapredStatement ok to avoid all this problems of syntax ok @VaisakhMA
1

there is error in your mysql syntax

 String query="insert into stdetails (regno,nam,gender) values(?,?,?)";
 PreparedStatement preparedStmt2 = con.prepareStatement(query);
 preparedStmt2.setInt (1," ");
 preparedStmt2.setString (2," ");
 preparedStmt2.setString(3, " ");
 preparedStmt2.execute();

like this you can add more columns also

2 Comments

Why setString if the first columns are int values
Can you really use a string in setInt?

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.