我试图将字符串数组和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();
Run Code Online (Sandbox Code Playgroud)
更换
pStmnt.executeUpdate(query);
Run Code Online (Sandbox Code Playgroud)
通过
pStmnt.executeUpdate();
Run Code Online (Sandbox Code Playgroud)
并且在循环之前准备语句一次,而不是在每次迭代时一次又一次地重新准备它.
| 归档时间: |
|
| 查看次数: |
138 次 |
| 最近记录: |