1

I have a database and I need to insert a new record there. I try to use PreparedStatement.

//Executing the INSERT query                
String addSql = "INSERT INTO savedforms (Patientfnr, Patientname) VALUES (?, ?)";       

//PreparedStatement for INSERT sql
java.sql.PreparedStatement stmt1 = connection.prepareStatement(addSql);

stmt1.setString(1, patientnumber);
stmt1.setString(2, patientname);


//Execute query 
stmt1.executeUpdate(addSql);    

and get the following error:

com.mysql.jdbc.exceptions.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 '?, ?)' at line 1

how it can be fixed?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
vlcod
  • 229
  • 2
  • 3
  • 13

2 Answers2

2

Change

stmt1.executeUpdate(addSql); 

to

stmt1.executeUpdate(); 

You've already set up your prepared statement, simply call executeUpdate() on it.

Mohammed Aouf Zouag
  • 17,042
  • 4
  • 41
  • 67
0

Please note that as the method declares that it should not be called on PreparedStatement or CallableStatement.

Check the javadoc for this kind of method