zoukankan      html  css  js  c++  java
  • JDBC05----事务与批处理

    一. 事务的基本概念

    1. 什么是事务

    事务是不可分割的操作,每条sql语句都是一个事务;事务只对DML语句有效,对DQL无效。

    2. 事务的ACID

    • 原子性(Atomicity)
    • 一致性(Consistency)
    • 隔离性(Isolation)
    • 持久性(Durability)

    3. 假设这里有一个需求体现了事务

    二. 批处理

    1. 什么是批处理

    一次性执行多条sql语句,允许多条语句一次性提交给数据库批量处理,比单独提交处理效率搞。

    2. 批处理方法

    addBatch(String):添加需要批处理的SQL语句;

    executeBatch():执行批处理

    3. 举例

    (1)新建立一个类BatchTest

     1 package com.test.jdbctest.dao.test;
     2 
     3 import java.sql.Connection;
     4 import java.sql.PreparedStatement;
     5 import java.sql.SQLException;
     6 
     7 
     8 import com.test.jdbctest.util.JDBCUtil;
     9 
    10 public class BatchTest {
    11     public static void main(String args[]) {
    12         Connection connection=JDBCUtil.getConnection();
    13         String sql="insert into table1(name,age) values(?,?)";
    14         PreparedStatement ps;
    15         try {
    16             ps = connection.prepareStatement(sql);
    17             long begin=System.currentTimeMillis();
    18             for(int i=0;i<1000;i++) {
    19                 ps.setString(1,"zs" );
    20                 ps.setInt(2, 10);
    21                 ps.executeUpdate();
    22             }
    23             long end=System.currentTimeMillis();
    24             long time=end-begin;
    25             System.out.println(time);
    26             JDBCUtil.close(connection, ps, null);
    27         } catch (SQLException e) {
    28             // TODO Auto-generated catch block
    29             e.printStackTrace();
    30         }
    31         
    32     }
    33 
    34 }
    View Code

    4481

    倘若使用批处理:

    note:需要在url中添加一个参数:

    public static String url="jdbc:mysql://localhost:3306/test?rewriteBatchedStatements=true";

     1 package com.test.jdbctest.dao.test;
     2 
     3 import java.sql.Connection;
     4 import java.sql.PreparedStatement;
     5 import java.sql.SQLException;
     6 
     7 
     8 import com.test.jdbctest.util.JDBCUtil;
     9 
    10 public class BatchTest {
    11     public static void main(String args[]) {
    12         Connection connection=JDBCUtil.getConnection();
    13         String sql="insert into table1(name,age) values(?,?)";
    14         PreparedStatement ps;
    15         try {
    16             ps = connection.prepareStatement(sql);
    17             long begin=System.currentTimeMillis();
    18             for(int i=0;i<1000;i++) {
    19                 ps.setString(1,"zs" );
    20                 ps.setInt(2, 10);
    21                 //添加到批处理
    22                 ps.addBatch();
    23             }
    24             //执行批处理
    25             ps.executeBatch();
    26             long end=System.currentTimeMillis();
    27             long time=end-begin;
    28             System.out.println(time);
    29             JDBCUtil.close(connection, ps, null);
    30         } catch (SQLException e) {
    31             // TODO Auto-generated catch block
    32             e.printStackTrace();
    33         }
    34         
    35     }
    36 
    37 }
    View Code

    42

     

    参考文献

    https://ke.qq.com/course/339214

  • 相关阅读:
    从源码分析 XtraBackup 的备份原理
    移动端 SDK 开发经验总结及梳理
    spring boot jar包开机自启
    在Simulink中添加VeriStand支持
    java 启动脚本
    Docker容器日志管理最佳实践
    docker 日志限制或者删除
    网盘搜索
    tuple c++
    google原版:Debugging WebAssembly with modern tools
  • 原文地址:https://www.cnblogs.com/Hermioner/p/10235842.html
Copyright © 2011-2022 走看看