MySQL JDBC Statement.executeBatch举例分析
发表于:2025-02-23 作者:千家信息网编辑
千家信息网最后更新 2025年02月23日,本篇内容主要讲解"MySQL JDBC Statement.executeBatch举例分析",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"MySQL JD
千家信息网最后更新 2025年02月23日MySQL JDBC Statement.executeBatch举例分析
本篇内容主要讲解"MySQL JDBC Statement.executeBatch举例分析",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"MySQL JDBC Statement.executeBatch举例分析"吧!
mport java.sql.*;/** * @Author cherrishccl * @Date 2020/9/4 17:01 * @Version 1.0 * @Description * 用JDBC执行批量提交正确方式: * 1. Statement stmt = conn.createStatement(); * for(int i = 0; i < 10000; i++){ * String batchSql = "insert into t_user(name, sex, age) values('name', 'F', 22)"; * stmt.addBatch(batchSql); * } * stmt.executeBatch(); * * 2. 连接参数添加&rewriteBatchedStatements=true * PreparedStatement stmt = conn.prepareStatement("insert into t_user(name, sex, age) values (?, ?, ?)"); * for(int i = 0; i < 10000; i++){ * stmt.setString(1, "name"); * stmt.setString(2, "F"); * stmt.setString(3, 22); * stmt.addBatch(); * } */public class BatchTest1 { public static void main(String[] args) throws SQLException, ClassNotFoundException { String driver = "com.mysql.cj.jdbc.Driver"; String url = "jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false" + "&allowMultiQueries=true&rewriteBatchedStatements=true"; String url1 = "jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&characterEncoding=utf8&useUnicode=true&useSSL=false&allowMultiQueries=true"; String username = "root"; String password = "123456"; /************************插入10000条数据**************************************/ // url 和 url1 效率差不多// batch2(driver, url, username, password); // 平均耗时4175// batch2(driver, url1, username, password); // 平均耗时4156 // url 和 url1 效率差不多// batch3(driver, url, username, password); // 平均耗时1951// batch3(driver, url1, username, password); // 平均耗时1873 /***********************插入100条数据***************************************/ // url 和 url1 效率差不多// batch4(driver, url, username, password); // 平均耗时7730// batch4(driver, url1, username, password); // 平均耗时6208 // url 和 url1 效率差不多// batch5(driver, url, username, password); // 平均耗时6096// batch5(driver, url1, username, password); // 平均耗时6056 // url 和 url1 效率差不多// batch6(driver, url, username, password); // 平均耗时6466// batch6(driver, url1, username, password); // 平均耗时6310 // url 和 url1 效率差不多// batch7(driver, url, username, password); // 平均耗时5969// batch7(driver, url1, username, password); // 平均耗时6201 /***********************插入10000条数据***************************************/// batch7(driver, url, username, password); // 平均耗时990// batch7(driver, url1, username, password); // 平均耗时>>>>>120000// batch8(driver, url, username, password); // 平均耗时602// batch8(driver, url1, username, password); // 平均耗时>>>>>120000 } private static Connection connect(String driver, String url, String username, String password) throws ClassNotFoundException, SQLException { Class.forName(driver); return DriverManager.getConnection(url, username, password); } private static void batch2(String driver, String url, String username, String password) throws SQLException, ClassNotFoundException { Connection conn = connect(driver, url, username, password); Statement stmt = conn.createStatement(); long start = System.currentTimeMillis(); for(int i = 0; i < 10000; i++){ String batchSql = "insert into t_user(user_name, age, salary, max_size) values (" + "'name" + i + "'," + i + "," + i + "," + i + ")"; stmt.addBatch(batchSql); } stmt.executeBatch(); // 4175 System.out.println("耗时======>" + (System.currentTimeMillis() - start)); } private static void batch3(String driver, String url, String username, String password) throws SQLException, ClassNotFoundException { Connection conn = connect(driver, url, username, password); Statement stmt = conn.createStatement(); conn.setAutoCommit(false); long start = System.currentTimeMillis(); for(int i = 0; i < 10000; i++){ String batchSql = "insert into t_user(user_name, age, salary, max_size) values (" + "'name" + i + "'," + i + "," + i + "," + i + ")"; stmt.addBatch(batchSql); } stmt.executeBatch(); conn.commit(); // 1398 System.out.println("耗时======>" + (System.currentTimeMillis() - start)); } private static void batch4(String driver, String url, String username, String password) throws SQLException, ClassNotFoundException { Connection conn = connect(driver, url, username, password); Statement stmt = conn.prepareStatement("select * from dual"); long start = System.currentTimeMillis(); for(int i = 0; i < 100; i++){ String batchSql = "insert into t_user(user_name, age, salary, max_size) values (" + "'name" + i + "'," + i + "," + i + "," + i + ")"; stmt.addBatch(batchSql); } stmt.executeBatch(); // 7730 System.out.println("耗时======>" + (System.currentTimeMillis() - start)); } private static void batch5(String driver, String url, String username, String password) throws SQLException, ClassNotFoundException { Connection conn = connect(driver, url, username, password); Statement stmt = conn.prepareStatement("select * from dual"); conn.setAutoCommit(false); long start = System.currentTimeMillis(); for(int i = 0; i < 100; i++){ String batchSql = "insert into t_user(user_name, age, salary, max_size) values (" + "'name" + i + "'," + i + "," + i + "," + i + ")"; stmt.addBatch(batchSql); } stmt.executeBatch(); conn.commit(); // 7730 System.out.println("耗时======>" + (System.currentTimeMillis() - start)); } private static void batch6(String driver, String url, String username, String password) throws SQLException, ClassNotFoundException { Connection conn = connect(driver, url, username, password); PreparedStatement stmt = conn.prepareStatement("select * from dual"); long start = System.currentTimeMillis(); for(int i = 0; i < 100; i++){ String batchSql = "insert into t_user(user_name, age, salary, max_size) values (" + "'name" + i + "'," + i + "," + i + "," + i + ")"; stmt.addBatch(batchSql); } stmt.executeBatch(); // 6162 System.out.println("耗时======>" + (System.currentTimeMillis() - start)); } private static void batch7(String driver, String url, String username, String password) throws SQLException, ClassNotFoundException { Connection conn = connect(driver, url, username, password); PreparedStatement stmt = conn.prepareStatement("select * from dual"); conn.setAutoCommit(false); long start = System.currentTimeMillis(); for(int i = 0; i < 100; i++){ String batchSql = "insert into t_user(user_name, age, salary, max_size) values (" + "'name" + i + "'," + i + "," + i + "," + i + ")"; stmt.addBatch(batchSql); } stmt.executeBatch(); conn.commit(); // 6162 System.out.println("耗时======>" + (System.currentTimeMillis() - start)); } private static void batch7(String driver, String url, String username, String password) throws SQLException, ClassNotFoundException { Connection conn = connect(driver, url, username, password); PreparedStatement stmt = conn.prepareStatement("insert into t_user(user_name, age, salary, max_size) values (?, ?, ?, ?)"); long start = System.currentTimeMillis(); for(int i = 0; i < 10000; i++){ stmt.setInt(1, i + 1); stmt.setInt(2, i + 1000); stmt.setInt(3, i + 1); stmt.setInt(4, i); stmt.addBatch(); } stmt.executeBatch(); // 6162 System.out.println("耗时======>" + (System.currentTimeMillis() - start)); } private static void batch8(String driver, String url, String username, String password) throws SQLException, ClassNotFoundException { Connection conn = connect(driver, url, username, password); PreparedStatement stmt = conn.prepareStatement("insert into t_user(user_name, age, salary, max_size) values (?, ?, ?, ?)"); conn.setAutoCommit(false); long start = System.currentTimeMillis(); for(int i = 0; i < 10000; i++){ stmt.setInt(1, i + 1); stmt.setInt(2, i + 1000); stmt.setInt(3, i + 1); stmt.setInt(4, i); stmt.addBatch(); } stmt.executeBatch(); conn.commit(); // 6162 System.out.println("耗时======>" + (System.currentTimeMillis() - start)); }}
到此,相信大家对"MySQL JDBC Statement.executeBatch举例分析"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!
分析
内容
学习
实用
更深
兴趣
参数
实用性
实际
操作简单
方式
方法
更多
朋友
网站
频道
查询
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
云数据库适合哪些行业
普法网络安全手抄报
承接辅助软件开发
警示卡通人物网络安全
为何要关注网络安全
互联网科技企业抢占小商贩
网络安全保卫的著作
软件开发用英语怎么说
渭南高新区网络安全培训
关于网络安全的手抄报的内容
白云高端网络安全服务
加固携行式服务器
我的世界服务器种子怎么隐藏
网络安全工程师的硬性要求
国家网络安全法什么时候颁布
软件开发中出现开发时间过长
山西职业技术学院计算机网络技术
网络安全等级保护申请条件
什么样的云数据库好
统计局专版连接不到服务器
华为服务器管理网口在哪
人民医院软件开发
数据库简繁转换函数
网络安全法 课件
建模软件开发优势
网络安全法31-38
陕西服务器机柜哪里买
国产实时数据库哪家好
租服务器 10万一天
读懂网络安全观人民出版社