【问题标题】:What is wrong with PreparedStatement? [duplicate]PreparedStatement 有什么问题? [复制]
【发布时间】:2016-04-15 14:22:30
【问题描述】:

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

如何解决?

【问题讨论】:

    标签: java mysql jdbc prepared-statement


    【解决方案1】:

    改变

    stmt1.executeUpdate(addSql); 
    

    stmt1.executeUpdate(); 
    

    您已经设置了准备好的语句,只需调用executeUpdate()就可以了。

    【讨论】:

      【解决方案2】:

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

      检查 javadoc 是否有这种方法

      【讨论】:

        猜你喜欢
        • 2012-07-16
        • 2019-04-11
        • 2016-03-20
        • 2020-06-17
        • 1970-01-01
        • 2019-12-16
        • 2023-03-25
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多