当前位置:网站首页>MySQL batch insert demo
MySQL batch insert demo
2022-07-24 05:43:00 【My benefits remain the same】
Mysql Batch insert demo Code
public static void main(String[] args) throws ClassNotFoundException, SQLException {
final String url = "jdbc:mysql://127.0.0.1/wvp?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
final String name = "com.mysql.cj.jdbc.Driver";
final String user = "root";
final String password = "1234";
Connection conn = null;
// Specify connection type
Class.forName(name);
// Get the connection
conn = DriverManager.getConnection(url, user, password);
if (conn != null) {
System.out.println(" Get connection succeeded ");
// The insert
batchInsert(conn);
} else {
System.out.println(" Failed to get connection ");
}
}
public static void batchInsert(Connection conn) {
// Starting time
Long begin = System.currentTimeMillis();
// sql Prefix
String sqlPrefix = "INSERT INTO student (name, age) VALUES ";
try {
// preservation sql suffix
StringBuffer sqlSuffix = new StringBuffer();
// Set the transaction to non auto commit
conn.setAutoCommit(false);
// Prepare to execute statement
PreparedStatement pst = (PreparedStatement) conn.prepareStatement(" ");
// The outer loop , Total number of transactions committed
for (int i = 1; i <= 100; i++) {
sqlSuffix = new StringBuffer();
// The first j Second submission step size
for (int j = 1; j <= 100000; j++) {
// structure SQL suffix
sqlSuffix.append("('" + "cxx" + j + "'," + 1 + "),");
}
// Build complete SQL
String sql = sqlPrefix + sqlSuffix.substring(0, sqlSuffix.length() - 1);
// Add execution SQL
pst.addBatch(sql);
// Perform the operation
pst.executeBatch();
// Commit transaction
conn.commit();
// Clear the last data added
sqlSuffix = null;
}
// First class connection
pst.close();
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
// End time
Long end = System.currentTimeMillis();
// Time consuming
System.out.println("1000 Ten thousand data inserts take time : " + (end - begin) + "ms");
System.out.println(" Insert complete ");
}边栏推荐
- MySQL误操作后如何快速恢复数据
- Penetration testing knowledge - industry terminology
- 【vsphere高可用】主机和虚拟机故障监测工作原理
- Flink sql-client.sh使用
- 推荐一款完全开源,功能丰富,界面精美的商城系统
- Principle of fusdt liquidity pledge mining development logic system
- [data mining] zero foundation entry decision tree
- 达梦数据库_常用的用户管理命令
- Insanity:1(Insanity-Hosting)靶机渗透 —Vulnhub
- Flink重启策略
猜你喜欢

Imitate Baidu API of Baidu map page of a website

关于DAO流动性双币质押挖矿开发原理分析

spark 广播变量和累加器使用和原理

【activiti】组任务

Principle of fusdt liquidity pledge mining development logic system

【activiti】流程变量

【mycat】mycat搭建读写分离

Creation and generation of SVG format map in Heilongjiang Province

haclabs: no_ Name (hl.ova) target penetration vulnhub

Flink watermark mechanism
随机推荐
达梦数据库_DISQL下各种连接数据库的方法和执行SQL、脚本的方法
ODS、数据集市、数据仓库的异同点
How can the multiple-choice and single choice results of PHP be displayed in the foreground?
[vSphere high availability] host failover
CESS 测试网上线!首个提供多元应用场景的去中心化存储网络
Unknown collation: ‘utf8mb4_0900_ai_ci‘的解决方法
Scarcity in Web3: how to become a winner in a decentralized world
[data mining] zero foundation entry decision tree
数据集成的两种架构:ELT和ETL
推荐一款完全开源,功能丰富,界面精美的商城系统
Principle of fusdt liquidity pledge mining development logic system
LP双币流动性质押挖矿系统逻辑开发分析
Flink函数(1):rich function
Vscode configuring autoprefixer
haclabs: no_ Name (hl.ova) target penetration vulnhub
plsql查询数据乱码
【mycat】mycat配置文件
Insanity:1 (insanity hosting) target penetration vulnhub
Useref create dynamic reference
ntp错误: no server suitable for synchronization found