使用executeBatch vs运算符的MySQL DML操作

时间:2016-05-26 14:11:47

标签: mysql jdbc sql-update

如果我必须将表格的字段更新为100个记录的特定(相同)值。以下哪种方法更快更优先?

1。使用准备好的声明

String query = "update employee set status='A' where empId = ?"; 
PreparedStatement pStmt = con.prepareStatement(query);
for(int i=0; i<list.size(); i++){
    Long empId = list.get(i);
    pStmt.setLong(1, empId);
    pStmt.addBatch();
}
pStmt.executeBatch();

2。在运营商中使用

StringBuilder query = "update employee set status='A' where empId in (";
for(int i=0; i<list.size(); i++){
    Long empId = list.get(i);
    query.append(empId).append(",");
}
query.setLength(query.length() - 1);
query.append(")");
Statement stmt = con.createStatement();
stmt.executeUpdate(query.toString());

0 个答案:

没有答案