教你如何6秒钟往MySQL插入100万条数据!然后删库跑路!

教你如何6秒钟往MySQL插入100万条数据!然后删库跑路!

由于我用的mysql 8版本,所以增加了Timezone,然后就可以了

前提是要自己建好库和表。 数据库test, 表user, 三个字段: u_id、u_name、u_pwd,然后主键自增 

 conn = DriverManager.getConnection("jdbc:mysql:///test?" + "&rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai", "root", "123456");

运行结果如下: 

代码如下:

package rs.dms.test;

import java.sql.*;

/**
 * @author : Bei-Zhen
 * @date : 2020-08-24 0:43
 */
public class JDBC2 {

    //static int count = 0;

    public static void main(String[] args) {

        long start = System.currentTimeMillis();
        conn();
        long end = System.currentTimeMillis();
        System.out.println("耗时:" + (end - start) / 1000 + "秒");
    }

    public static void conn() {
        //1.导入驱动jar包
        //2.注册驱动(mysql5之后的驱动jar包可以省略注册驱动的步骤)
        //Class.forName("com.mysql.jdbc.Driver");
        //3.获取数据库连接对象
        Connection conn = null;
        PreparedStatement pstmt = null;
        {
            try {
                //"&rewriteBatchedStatements=true",一次插入多条数据,只插入一次
                conn = DriverManager.getConnection("jdbc:mysql:///test?" + "&rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai", "root", "123456");
                //4.定义sql语句
                String sql = "insert into user values(default,?,?)";
                //5.获取执行sql的对象PreparedStatement
                pstmt = conn.prepareStatement(sql);
                //6.不断产生sql
                for (int i = 0; i < 1000000; i++) {
                    pstmt.setString(1, (int) (Math.random() * 1000000) + "");
                    pstmt.setString(2, (int) (Math.random() * 1000000) + "");
                    pstmt.addBatch();
                }
                //7.往数据库插入一次数据
                pstmt.executeBatch();
                System.out.println("添加1000000条信息成功!");

            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                //8.释放资源
                //避免空指针异常
                if (pstmt != null) {
                    try {
                        pstmt.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }

                if (conn != null) {
                    try {
                        conn.close();
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

    }

}
原文地址:https://www.cnblogs.com/myfrank/p/15703023.html