JDBC批量插入
package com.jdbc.test; import java.sql.*; public class Demo02 { PreparedStatement preparedStatement = null; ResultSet resultSet = null; Connection connection = null; public static void main(String[] args) throws SQLException, ClassNotFoundException { Demo02 demo02 = new Demo02(); demo02.connect(); } public void connect() throws ClassNotFoundException, SQLException { Class.forName("com.mysql.jdbc.Driver"); connection = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/jdjk?useSSL=false", "root", "kkk"); /** * 批量插入 */ long start = System.currentTimeMillis(); connection.setAutoCommit(false); preparedStatement = connection.prepareStatement("insert into jdjk_java(name,age,email) values (?,?,?)"); for (int i = 0; i <= 2000000; i++) { preparedStatement.setString(1, "javademo"); preparedStatement.setInt(2, i); preparedStatement.setString(3, "wangzhilei@jd.com"); preparedStatement.addBatch(); } preparedStatement.executeBatch(); connection.commit(); long end = System.currentTimeMillis(); System.out.println("插入时长" + (end - start)); preparedStatement.close(); // 关闭连接 connection.close(); } }
技术改变一切