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
  • 90,369
  • 161
  • 124
  • 175
vlcod
  • 199
  • 2
  • 2
  • 12

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
  • 16,747
  • 3
  • 39
  • 66
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