查看“JDBC批量插入数据的操作”的源代码
←
JDBC批量插入数据的操作
跳到导航
跳到搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看和复制此页面的源代码。
https://www.bilibili.com/video/BV1eJ411c7rf/?p=32 === 建表 === <syntaxhighlight lang="sql"> create table goods( id int primary key auto_increment, name varchar(25) ); </syntaxhighlight> === 批量插入的方式1 === <syntaxhighlight lang="java"> Connection connection = JDBCUtils.getConnection(); Statement statement = connection.createStatement(); for (int i = 1; i <= 20000; i++) { String sql = "insert into goods(name) values('name_" + i + "')"; statement.execute(sql); } </syntaxhighlight> === 批量插入的方式2 === <syntaxhighlight lang="java"> //批量插入的方式二:使用 PreparedStatement @Test public void testInsert1() { Connection connection = null; PreparedStatement preparedStatement = null; try { long start = System.currentTimeMillis(); connection = JDBCUtils.getConnection(); String sql = "insert into goods(name) values(?)"; preparedStatement = connection.prepareStatement(sql); for (int i = 1; i <= 20000; i++) { preparedStatement.setObject(1, "name_" + i); preparedStatement.execute(); } long end = System.currentTimeMillis(); System.out.printf("花费的时间为:%d\n", end - start); } catch (SQLException e) { throw new RuntimeException(e); } catch (Exception e) { throw new RuntimeException(e); } finally { JDBCUtils.closeResource(connection, preparedStatement); } } </syntaxhighlight><syntaxhighlight lang="console"> 花费的时间为:19875 </syntaxhighlight> https://www.bilibili.com/video/BV1eJ411c7rf/?p=33 === 批量插入的方式3 === ==== jdbc.properties ==== <syntaxhighlight lang="properties"> user=root password=123456 url=jdbc:mysql://localhost:3306/jdbc_test?rewriteBatchedStatements=true driverClass=com.mysql.cj.jdbc.Driver </syntaxhighlight><syntaxhighlight lang="java"> /** * 批量插入的方式三: * 1.addBatch()、executeBatch()、clearBatch() * 2.MySQL 服务器默认是关闭批处理的,我们需要通过一个参数,让 MySQL 开启批处理的支持。 * ?rewriteBatchedStatements=true 写在配置文件的 URL 后面 * 3.使用更新的 MySQL 驱动:>= mysql-connector-java-5.1.37-bin.jar */ @Test public void testInsert2() { Connection connection = null; PreparedStatement preparedStatement = null; try { long start = System.currentTimeMillis(); connection = JDBCUtils.getConnection(); String sql = "insert into goods(name) values(?)"; preparedStatement = connection.prepareStatement(sql); for (int i = 1; i <= 20000; i++) { preparedStatement.setObject(1, "name_" + i); //1.“攒” SQL preparedStatement.addBatch(); if (i % 500 == 0) { //2.执行 preparedStatement.executeBatch(); //3.清空batch preparedStatement.clearBatch(); } } long end = System.currentTimeMillis(); System.out.printf("花费的时间为:%d\n", end - start); } catch (SQLException e) { throw new RuntimeException(e); } catch (Exception e) { throw new RuntimeException(e); } finally { JDBCUtils.closeResource(connection, preparedStatement); } } </syntaxhighlight> ==== 清空数据 ==== <syntaxhighlight lang="sql"> truncate table goods; </syntaxhighlight> ==== 运行验证 ==== <syntaxhighlight lang="console"> 花费的时间为:864 </syntaxhighlight> === 批量插入方式4 ===
返回至
JDBC批量插入数据的操作
。
导航菜单
个人工具
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
导航
首页
Spring Boot 2 零基础入门
Spring Cloud
Spring Boot
设计模式之禅
VUE
Vuex
Maven
算法
技能树
Wireshark
IntelliJ IDEA
ElasticSearch
VirtualBox
软考
正则表达式
程序员精讲
软件设计师精讲
初级程序员 历年真题
C
SQL
Java
FFmpeg
Redis
Kafka
MySQL
Spring
Docker
JMeter
Apache
Linux
Windows
Git
ZooKeeper
设计模式
Python
MyBatis
软件
数学
PHP
IntelliJ IDEA
CS基础知识
网络
项目
未分类
MediaWiki
镜像
问题
健身
国债
英语
烹饪
常见术语
MediaWiki帮助
工具
链入页面
相关更改
特殊页面
页面信息