public static void main(String[] args) throws ClassNotFoundException, SQLException { String url = "jdbc:mysql://118.89.248.17:29019/sem_dev?rewriteBatchedStatements=true&characterEncoding=UTF-8&useSSL=false"; String user = "root"; String password = "root"; memberTokenInsert(url,user,password); } public static void memberinsert(String url,String user,String password){ Connection conn = null; PreparedStatement pstm = null; ResultSet rt = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, user, password); String sql = "INSERT INTO sem_member(user_id,username,password,salt,mobile,status,create_time,truename,avatar,sex,email,login_num," + "login_ip,is_del,last_login_ip,login_time,last_login_time,ischeck,member_rank_points,member_papers,member_type,member_source,template_num,activity_num)" + " VALUES(?,CONCAT('测试用户',?),?,?,?,?,?,CONCAT('真实姓名',?),?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; pstm = conn.prepareStatement(sql); conn.setAutoCommit(false); Long startTime = System.currentTimeMillis(); Random rand = new Random(); int a, b, c, d, e, f, g; for (int i = 1; i <= 1000000; i++) { String uuid = UUID.randomUUID().toString().replaceAll("-", ""); pstm.setString(1, uuid); pstm.setString(2, String.valueOf(i)); pstm.setString(3, String.valueOf(i)); pstm.setString(4, String.valueOf(i)); a = rand.nextInt(10); b = rand.nextInt(10); c = rand.nextInt(10); d = rand.nextInt(10); e = rand.nextInt(7); f = rand.nextInt(3); g = rand.nextInt(100); pstm.setString(5, "188" + a + "88" + b + c + "66" + d); pstm.setInt(6, 1); pstm.setDate(7, new java.sql.Date(System.currentTimeMillis())); pstm.setString(8, String.valueOf(i)); pstm.setString(9, "http://sso.ckrsc.com/avatar/user/FES6RUYBWBFPZKK4KXBNHLSXLQ.jpg"); pstm.setInt(10, 1); pstm.setString(11, "98" + a + "35" + b + c + "2" + "@qq.com"); pstm.setLong(12, g); pstm.setString(13, "223." + a + b + ".71." + c + d); pstm.setInt(14, 0); pstm.setString(15, "117." + a + b + ".128." + c + d); pstm.setDate(17, new java.sql.Date(System.currentTimeMillis())); pstm.setDate(16, new java.sql.Date(System.currentTimeMillis())); pstm.setInt(18, e); pstm.setInt(19, g); pstm.setString(20, "{\"frontPath\":\"group1/M00/00/06/oYYBAFvJmpSAKVZcAABHyI0AzhQ335.jpg\",\"behindPath\":\"group1/M00/00/06/oYYBAFvJmpiARsF1AABM6H2r740286.jpg\"}"); pstm.setInt(21, f); pstm.setInt(22, f); pstm.setInt(23, f); pstm.setInt(24, f); pstm.addBatch(); } pstm.executeBatch(); conn.commit(); Long endTime = System.currentTimeMillis(); System.out.println("OK,用时:" + (endTime - startTime)); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e); } finally { closeResource(pstm,conn); } }
插入百万性能测试数据
最新推荐文章于 2024-04-18 17:21:00 发布