Statement 就是一种批处理
PreparedStatement 实现批处理是一条一条增加。
Statement 实现批处理是可以多条同时增加。
增加批处理的方法为:
addBatch() //将一组参数添加到此 PreparedStatement 对象的批处理命令中
例:addBatch("select * from student");
executeBatch() // 将一批命令提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。
它的返回类型是数组。
例:int[] rows =stmt.executeBatch();
for(int i:rows)
System.out.println(i);
PreparedStatement 实现批处理是一条一条增加。
Statement 实现批处理是可以多条同时增加。
增加批处理的方法为:
addBatch() //将一组参数添加到此 PreparedStatement 对象的批处理命令中
例:addBatch("select * from student");
executeBatch() // 将一批命令提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。
它的返回类型是数组。
例:int[] rows =stmt.executeBatch();
for(int i:rows)
System.out.println(i);
本文介绍了在数据库操作中使用批处理的方法,对比了Statement与PreparedStatement在实现批处理时的区别。通过addBatch()方法可以将多个SQL命令添加到批处理中,而executeBatch()则用于执行这些命令并将结果返回。
626

被折叠的 条评论
为什么被折叠?



