我正在尝试将字符串数组和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();
答案 0 :(得分:5)
替换
pStmnt.executeUpdate(query);
通过
pStmnt.executeUpdate();
在循环之前准备一次语句,而不是在每次迭代时一次又一次地重新准备它。