Vaisakh MA
Vaisakh MA

Reputation: 104

JAVA jdbc Eclipse SQLException

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

Upvotes: 1

Views: 263

Answers (2)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 60046

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.

Upvotes: 1

user6409042
user6409042

Reputation:

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

Upvotes: 1

Related Questions