我知道最佳做法是使用固定数量的参数,然后使用preparedStatement.setParam(xxx)
定义它们。
如果我有一个学生ID列表,我想更新每个学生数据行的列,该怎么办:
update student set student_grade=FAIL where student_id in (?)
其中?
将是我的列表对象中的学生ID列表。这样做最好的方法是什么?
答案 0 :(得分:0)
/ *我宁愿使用单独的更新并循环使用它们而不是使用IN运算符,类似于GriffeyDog的建议。建议可能将批量更新拆分为1000或更少。* /
/ *假设al是包含student_ids列表的arraylist,并假设student_ids数据类型为int * /
String updateTableSQL = "update student set student_grade='FAIL' where student_id = (?) ";
PreparedStatement preparedStatement = dbConnection.prepareStatement(updateTableSQL);
for (int i=0;i<al.size();i++){
preparedStatement.setInt(1,al.get(i) );
preparedStatement.addBatch();
}
preparedStatement.executeBatch();
dbConnection.commit();