【问题标题】:error on placeholder (?,?) [duplicate]占位符(?,?)错误[重复]
【发布时间】:2015-06-26 14:15:38
【问题描述】:

我正在尝试将字符串数组和 int 数组传递给数据库。我正在尝试的代码如下,但这总是显示错误

您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 '?,?)' 附近使用正确的语法

Connection connection = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/javabase", "java", "password");
CompareAndCount compareAndCount = new CompareAndCount();
ReadWord bagOfWords = new ReadWord();
String[] listOfWords = bagOfWords.openFile();
int[] count =compareAndCount.compareWords();
for (int i = 0; i < listOfWords.length; i++) {
    String query = "INSERT INTO angry(tagwords,termfrequency) VALUES(?,?)";
    PreparedStatement pStmnt = (PreparedStatement) connection.prepareStatement(query);
    pStmnt.setString(1, listOfWords[i]);
    pStmnt.setLong(2, count[i]);
    pStmnt.executeUpdate(query);
}
connection.close();

【问题讨论】:

    标签: java mysql


    【解决方案1】:

    替换

    pStmnt.executeUpdate(query);
    

    pStmnt.executeUpdate();
    

    并在循环之前准备一次语句,而不是在每次迭代时一次又一次地重新准备它。

    【讨论】:

      猜你喜欢
      • 2017-12-24
      • 1970-01-01
      • 2018-04-15
      • 1970-01-01
      • 2015-09-04
      • 2015-04-18
      • 2013-11-27
      • 2017-11-09
      • 2014-01-30
      相关资源
      最近更新 更多