try{
Connection conn = getConnection();
String strUpdateQuery = "update payment_table set CREDIT_CARD_NO = ? where PAYMENT_KEY= ?";
PreparedStatement ps =conn.prepareStatement(strUpdateQuery);
for(int i=0;i<nodes.getLength();i++){
ps.setString(1,"524364OQNBQQ4291");
ps.setString(2,"20130215123757533280168");
ps.executeUpdate();
conn.commit();
}
}catch(SQLException e){
e.printStackTrace();
}
即使在我检查主键之后,甚至不更新单行也是正确的。
答案 0 :(得分:2)
尝试批量更新:
void batchUpdate() {
String strUpdateQuery = "UPDATE payment_table " +
"SET CREDIT_CARD_NO = ? " +
"WHERE PAYMENT_KEY= ?";
try (Connection conn = getConnection();
PreparedStatement ps = conn.prepareStatement(strUpdateQuery)) {
for (int i = 0; i < nodes.getLength(); i++) {
ps.setString(1, "524364OQNBQQ4291");
ps.setString(2, "20130215123757533280168");
ps.addBatch();
}
int[] updated = ps.executeBatch();
// can log updated rows from "updated"
// conn.commit(); in case autocommit set to false or used conn.setAutoCommit(false) somewhere
}
catch (SQLException e) {
e.printStackTrace();
}
}