第一种:采用Statement.addBatch(sql)方式实现批处理:
*
优点:可以向数据库发送多条不同的SQL语句。
*
缺点:
SQL语句没有预编译。
当向数据库发送多条语句相同,但仅参数不同的SQL语句时,需重复写上很多条SQL语句。例如:
Insert into user(name,password) values(‘aa’,’111
’);
Insert into user(name,password) values(‘bb’,’222
’);
Insert into user(name,password) values(‘cc’,’333
’);
Insert into user(name,password) values(‘dd’,’444
’);
实现批处理的第二种方式:
PreparedStatement.addBatch()
采用PreparedStatement.addBatch()实现批处理
*
优点:发送的是预编译后的SQL语句,执行效率高。
*缺点:只能应用在SQL语句相同,但参数不同的批处理中。因此此种形式的批处理经常用于在同一个表中批量插入数据,或批量更新表的数据
转载请注明原文地址: https://mac.8miu.com/read-508742.html