我试图将字符串数组和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();发布于 2015-06-26 14:18:33
替换
pStmnt.executeUpdate(query);通过
pStmnt.executeUpdate();并在循环之前准备一次语句,而不是在每次迭代时反复准备它。
https://stackoverflow.com/questions/31075507
复制相似问题