package com.itheima.jdbc.demo4;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class JDBC_pi_chu_li {
public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("com.mysql.cj.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/test?rewriteBatchedStatements=true", "root", "123456");
//web_test3是要访问的数据库 root是数据库的用户 123456是数据库的密码
// 默认情况下MySQL的批处理是没有开启的,需要在url后面拼接一个参数:rewriteBatchedStatements=true
System.out.println("成功加载MYSQL驱动");
PreparedStatement stmt = null;
String sql="insert into user values (null,?)";
stmt = conn.prepareStatement(sql);
for(int i=0;i<10000;i++) {
stmt.setString(1, "name"+i);
stmt.addBatch();
//添加批处理
if(i%1000==0) {
//添加批处理的数据是先把数据添加到内存中,为了避免数据太多,造成内存溢出,
//所以在数据1000条的时候执行批处理,避免发生内存溢出
stmt.executeBatch();
stmt.clearBatch();
}
}
conn.close();
stmt.close();
}
}
JDBC批处理
最新推荐文章于 2022-11-04 11:31:23 发布