package com.tt.mybatis.test; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Random; public class InsertIntoDBData { private static String url = "jdbc:mysql://localhost:3306/test?characterEncoding=utf-8"; private static String user = "root"; private static String password = "root"; public static void main(String[] args) { Connection conn = null; PreparedStatement ps = null; long starTime = System.currentTimeMillis(); try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url,user,password); //String sql1 = "insert into course(c_id,name) values (?,?)"; //String sql2 = "insert into students(id,name) values (?,?)"; //s_id是students表id,只有7w个数据 //c_id是course表id,只有100个数据 String sql3 = "insert into sc(sc_id,s_id,c_id,score) values (?,?,?,?)"; ps = conn.prepareStatement(sql3); Random rand = new Random(); for (int i = 1; i <= 700000; i++ ) { ps.setInt(1,i); ps.setInt(2,rand.nextInt(70000)); ps.setInt(3,rand.nextInt(100)); ps.setInt(4,rand.nextInt(100)); ps.addBatch(); } ps.executeBatch(); long endTime = System.currentTimeMillis(); System.out.println("OK,用时:"+ (endTime - starTime) + "毫秒"); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (ps != null ) { try { ps.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if (conn != null ) { try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } }运行结果:竟然要44分钟
原文地址:https://www.cnblogs.com/ttyyou/p/12344208.html
时间: 2024-10-27 12:40:09