public static void main(String[] args) throws SQLException {
/*
向user表中新增100条数据,可以使用批处理方式完成
要求每20条SQL语句为一批,整体发送到mysql执行!
*/
Connection connection = JDBCUtils.getConnection();
//创建SQL执行对象
String sql="insert into user values(null,?,?)";
PreparedStatement statement = connection.prepareStatement(sql);
for (int i = 1; i <= 105; i++) {
//String sql = "insert into user values(null, '光头强"+ i +"','123456"+i+"')";
String username="张三"+i;
String password="10"+i;
statement.setString(1,username);
statement.setString(2,password);
statement.addBatch(); // 添加SQL到批处理中
System.out.println();
// 每添加20条执行一次,可以避免java内存溢出!
if (i%20 == 0){
statement.executeBatch();
}
}
// 这条语句是为了确保批处理时,未被处理的语句执行!
statement.executeBatch();
}
批量处理sql语句
于 2023-07-09 19:56:23 首次发布