java 批处理

 
  1. import java.sql.BatchUpdateException;   
  2. import java.sql.Connection;   
  3. import java.sql.DriverManager;   
  4. import java.sql.PreparedStatement;   
  5. import java.sql.Statement;   
  6.   
  7. public class DemoPreparedStatementAddBatchMySQL {   
  8.   
  9.   public static Connection getConnection() throws Exception {   
  10.      String driver = "org.gjt.mm.mysql.Driver";   
  11.      String url = "jdbc:mysql://localhost/databaseName";   
  12.      String username = "root";   
  13.      String password = "root";   
  14.      Class.forName(driver);   
  15.      Connection conn = DriverManager.getConnection(url, username, password);   
  16.     return conn;   
  17.    }   
  18.   
  19.   public static void checkUpdateCounts(int[] updateCounts) {   
  20.     for (int i = 0; i < updateCounts.length; i++) {   
  21.       if (updateCounts[i] >= 0) {   
  22.          System.out.println("Successfully executed; updateCount=" + updateCounts[i]);   
  23.        } else if (updateCounts[i] == Statement.SUCCESS_NO_INFO) {   
  24.          System.out.println("Successfully executed; updateCount=Statement.SUCCESS_NO_INFO");   
  25.        } else if (updateCounts[i] == Statement.EXECUTE_FAILED) {   
  26.          System.out.println("Failed to execute; updateCount=Statement.EXECUTE_FAILED");   
  27.        }   
  28.      }   
  29.    }   
  30.   
  31.   public static void main(String[] args)throws Exception {   
  32.      Connection conn = null;   
  33.      PreparedStatement pstmt = null;   
  34.     try {   
  35.        conn = getConnection();   
  36.        conn.setAutoCommit(false);   
  37.        String query = "insert into add_batch_table(stringCol, intCol) values(?, ?)";   
  38.        pstmt = conn.prepareStatement(query);   
  39.        pstmt.setString(1, "1");   
  40.        pstmt.setInt(2, 100);   
  41.        pstmt.addBatch();   
  42.   
  43.        pstmt.setString(1, "2");   
  44.        pstmt.setInt(2, 200);   
  45.        pstmt.addBatch();   
  46.   
  47.        pstmt.setString(1, "3");   
  48.        pstmt.setInt(2, 300);   
  49.        pstmt.addBatch();   
  50.   
  51.       int[] updateCounts = pstmt.executeBatch();   
  52.        checkUpdateCounts(updateCounts);   
  53.        conn.commit();   
  54.      } catch (BatchUpdateException e) {   
  55.       int[] updateCounts = e.getUpdateCounts();   
  56.        checkUpdateCounts(updateCounts);   
  57.       try {   
  58.          conn.rollback();   
  59.        } catch (Exception e2) {   
  60.          e.printStackTrace();   
  61.        }   
  62.      } catch (Exception e) {   
  63.        e.printStackTrace();   
  64.      } finally {   
  65.        pstmt.close();   
  66.        conn.close();   
  67.      }   
  68.    }   
  69. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值