PreparedStatement:
1) addBatch()将一组参数添加到PreparedStatement对象内部。
2) executeBatch()将一批参数提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。
Statement:
1) addBatch(String sql)方法会在批处理缓存中加入一条sql语句。
2) executeBatch()执行批处理缓存中的所有sql语句。
注意:PreparedStatement中使用批量更新时,要先设置好参数后再使用addBatch()方法加入缓存。批量更新中只能使用更改、删除或插入语句。
- Statement批量处理和事务代码如下:
- package com.ambow.day20.jdbc.JDBCTestCommitAndRollback;
- import java.sql.Connection;
- import java.sql.SQLException;
- import java.sql.Statement;
- import com.ambow.day19.jdbc.util.JDBCConAndClo;
-
-
-
-
-
-
- public class StatementCommitAndRollbackTest {
- public static void main(String args[]) {
- Connection con = null;
- Statement stm = null;
- try {
- con = JDBCConAndClo.getConnectionBao();
- stm = con.createStatement();
- con.setAutoCommit(false);
-
- stm.addBatch("insert into student values(23,'tangbao','高数',100)");
- stm.addBatch("insert into student values(24,'王定','c#',98)");
- stm.addBatch("insert into student values(25,'王国云','java',90)");
- stm.addBatch("insert into student values(26,'溜出','英语',89)");
- stm.addBatch("insert into student values(27,'wqde','java',63)");
-
-
-
-
- stm.executeBatch();
- System.out.println("插入成功!");
-
- con.commit();
- System.out.println("提交成功!");
- con.setAutoCommit(true);
-
- } catch (SQLException e) {
- e.printStackTrace();
- try {
-
- if (!con.isClosed()) {
- con.rollback();
- System.out.println("提交失败,回滚!");
- con.setAutoCommit(true);
- }
- } catch (SQLException e1) {
- e1.printStackTrace();
- } finally {
- JDBCConAndClo.closeStatement(stm);
- JDBCConAndClo.closeConnection(con);
- }
- }
- }
- }
- PreparedStatement批量处理和事务代码如下:
- package com.ambow.day20.jdbc.JDBCTestCommitAndRollback;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import com.ambow.day19.jdbc.util.JDBCConAndClo;
-
-
-
-
-
-
-
- public class PreparedStatementCommitAndRollbackTest {
- public static void main(String args[]) {
- Connection con = null;
- PreparedStatement pstm = null;
-
- try {
-
- con = JDBCConAndClo.getConnectionBao();
-
-
- pstm = con.prepareStatement("insert into student values(?,?,?,?)");
- con.setAutoCommit(false);
-
- pstm.setInt(1, 33);
- pstm.setString(2,"wangqin");
- pstm.setString(3, "c++");
- pstm.setDouble(4, 78.5);
-
- pstm.addBatch();
-
- pstm.setInt(1, 34);
- pstm.setString(2,"wuytun");
- pstm.setString(3, "c");
- pstm.setDouble(4, 77);
-
- pstm.addBatch();
-
- pstm.setInt(1, 31);
- pstm.setString(2,"tetet");
- pstm.setString(3, "c++");
- pstm.setDouble(4, 90);
-
- pstm.addBatch();
-
- pstm.setInt(1, 32);
- pstm.setString(2,"liug");
- pstm.setString(3, "c");
- pstm.setDouble(4, 50);
-
- pstm.addBatch();
-
- pstm.executeBatch();
- System.out.println("插入成功!");
-
- con.commit();
- System.out.println("提交成功!");
- con.setAutoCommit(true);
-
- } catch (SQLException e) {
- e.printStackTrace();
- try {
-
- if(!con.isClosed()){
- con.rollback();
- System.out.println("插入失败,回滚!");
- con.setAutoCommit(true);
- }
- } catch (SQLException e1) {
- e1.printStackTrace();
- }
- }finally{
- JDBCConAndClo.closePreparedStatement(pstm);
- JDBCConAndClo.closeConnection(con);
- }
- }
- }
阅读(1178) | 评论(0) | 转发(0) |