核心方法:
PreparedStatement 对象的批处理命令中。
1、建立连接
Connection conn = QueryHelper.getConnection();
2、不自动commit提交
conn.setAutoCommit(false);
3、预编译sql语句,效率高
String updateSql = "update "+TABLE_CALCULATOR_ITEM_PRICE_DATA+" set item_price_value = ? , update_time =?, updater =? WHERE ID = ?";
PreparedStatement updateStmt = conn.prepareStatement(updateSql);
4、 将一组参数添加到此
PreparedStatement 对象的批处理命令中。
//更新一条记录
updateStmt.setDouble(1, calculatorItemPriceData.getItem_price_value());
updateStmt.setTimestamp(2,calculatorItemPriceData.getUpdate_time());
updateStmt.setString(3, calculatorItemPriceData.getUpdater());
updateStmt.setInt(4, calculatorItemPriceData.getId());
updateStmt.addBatch();
可以添加多条记录,如上面的添加代码
5、批量处理上面的数据
updateStmt.executeBatch();
6、关闭
updateStmt.close();
7、提交
conn.commit();
conn.setAutoCommit(true);