使用jdbc向mysql批量添加几十万数据
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分钟
相关推荐
Andrea0 2020-09-18
gaozhennan 2020-08-03
ASoc 2020-11-14
Cherishyuu 2020-08-19
dongtiandeyu 2020-08-18
CoderYYN 2020-08-16
大黑牛 2020-08-15
Dullonjiang 2020-08-11
mcvsyy 2020-08-02
zbcaicai 2020-07-29
AscaryBird 2020-07-27
liulin0 2020-07-26
ldcwang 2020-07-26
helloxusir 2020-07-25
娜娜 2020-07-20
pengpengflyjhp 2020-07-19
点滴技术生活 2020-07-19
人可 2020-07-18
chenjiazhu 2020-07-08