java preparedStatement.executeUpdate() 只插入最后一行
java preparedStatement.executeUpdate() only insert the last row
这是我的代码:
String insertQuery = "INSERT INTO Links(PageId, LinkId) values(?, ?)";
PreparedStatement preparedStatement = con.prepareStatement(insertQuery);
Vector<String> links = page.getLinks();
for (String string : links) {
Concept c = dbpedia.findConceptbyTitle(string);
if (c != null) {
preparedStatement.setInt(1, Integer.parseInt(page.getID()));
preparedStatement.setInt(2, c.id);
}
}
preparedStatement.executeUpdate();
奇怪的是,当我在 executeUpdate 之后设置断点时,只有最后一行插入 SQL table!
你每次只在最后一次执行时覆盖你的参数。使用批处理语句:
http://www.mkyong.com/jdbc/jdbc-preparedstatement-example-batch-update/
Bulk insert in Java using prepared statements batch update
添加preparedStatement.addBatch();在你离开循环之前。
并替换preparedStatement.executeUpdate();用 preparedStatement.executeBatch();.
这是我的代码:
String insertQuery = "INSERT INTO Links(PageId, LinkId) values(?, ?)";
PreparedStatement preparedStatement = con.prepareStatement(insertQuery);
Vector<String> links = page.getLinks();
for (String string : links) {
Concept c = dbpedia.findConceptbyTitle(string);
if (c != null) {
preparedStatement.setInt(1, Integer.parseInt(page.getID()));
preparedStatement.setInt(2, c.id);
}
}
preparedStatement.executeUpdate();
奇怪的是,当我在 executeUpdate 之后设置断点时,只有最后一行插入 SQL table!
你每次只在最后一次执行时覆盖你的参数。使用批处理语句: http://www.mkyong.com/jdbc/jdbc-preparedstatement-example-batch-update/
Bulk insert in Java using prepared statements batch update
添加preparedStatement.addBatch();在你离开循环之前。
并替换preparedStatement.executeUpdate();用 preparedStatement.executeBatch();.