package com.eg.egsc.egc.mapanalysisapp; import java.sql.Connection; import java.sql.Date; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.util.Random; /** * 批量操作+事务提交 **/ public class JdbcBatchInsertData { private static String url = "jdbc:postgresql://10.102.73.53:5432/test_db?rewriteBatchedStatements=true&characterEncoding=utf8&serverTimezone=GMT%2B8"; private static String user = "test"; private static String password = "test"; public static void main(String[] args) { Connection conn = null; PreparedStatement pstm = null; try { Class.forName("org.postgresql.Driver"); conn = DriverManager.getConnection(url, user, password); String sql = "INSERT INTO test_poi (id, park_id, project_id, longitude, latitude, type," + " project_short_desc, project_desc, tag_ids, version," + " pic_url, play_time, show_levels, play_number, wait_number," + " create_user, create_time, update_user, update_time, delete_flag) " + "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,? , ?, ?, ?, ?, ?, ?, ?)"; pstm = conn.prepareStatement(sql); // 先将命令的提交方式设为false,即手动提交conn.setAutoCommit(false); // 最后在所有命令执行完之后再提交事务conn.commit(); conn.setAutoCommit(false); Long startTime = System.currentTimeMillis(); Random rand = new Random(); // int a, b; // 插入10万条数据 for (int i = 1; i <= 100000; i++) { // 随机值 // a = rand.nextInt(10); // b = rand.nextInt(10); // 1代表的是列索引,i是值 pstm.setString(1, i + ""); pstm.setString(2, i + "");// 园区ID pstm.setString(3, i + ""); // 项目ID pstm.setString(4, "109.18" + i); // 经度 pstm.setString(5, "19.6" + i); // 纬度 pstm.setInt(6, 1);// type pstm.setString(7, "童话世界" + i); pstm.setString(8, "这里是童话世界" + i); pstm.setString(9, "1|2");// tag_id pstm.setInt(10, 1); // 版本 pstm.setString(11, "picUrl");// picUrl pstm.setInt(12, 4);// play_time pstm.setString(13, "3|5"); // show_levels pstm.setInt(14, 0);// play_number pstm.setInt(15, 0);// wait_number // Timestamp createDate = new Timestamp(System.currentTimeMillis()); // pstm.setTimestamp(16,createDate);// us_start_time // pstm.setTimestamp(17, createDate);// us_end_time pstm.setString(16, "admin");// create_user pstm.setDate(17, new Date(i));// create_time pstm.setString(18, "admin");// update_user pstm.setDate(19, new Date(i));// update_time pstm.setInt(20, 1);// delete_flag pstm.addBatch(); } pstm.executeBatch(); // 此处再commit conn.commit(); Long endTime = System.currentTimeMillis(); System.out.println("************************正在插入中,请稍等******************************"); System.out .println("批量插入用时:" + ((endTime - startTime)) + "毫秒" + "约等于" + ((endTime - startTime) / 1000) + "秒"); System.out.println("批量插入完成!"); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { if (pstm != null) { try { pstm.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); throw new RuntimeException(e); } } } } }