PreparedStatement 有什么问题?

What is wrong with PreparedStatement?

我有一个数据库,我需要在其中插入一条新记录。我尝试使用 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);    

并得到以下错误:

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

如何解决?

改变

stmt1.executeUpdate(addSql); 

stmt1.executeUpdate(); 

您已经设置了准备好的语句,只需对其调用 executeUpdate()

请注意,由于该方法声明不应在 PreparedStatement 或 CallableStatement 上调用它。

查看 javadoc 以了解此类方法