时间:2021-07-01 10:21:17 帮助过:16人阅读
public void batcheInsert(List<Map<String,Object>>listMap, String tableName){ try { String sql = ""; String sqlStr1 = "INSERT INTO "+tableName+" ("; String sqlStr2= ") VALUES ("; String sqlStr3 = ")"; String sqlKey = ""; String sqlValue = ""; for (Object key: listMap.get(0).keySet() ) { sqlKey+= key + ","; sqlValue += "?,"; } sqlKey = sqlKey.substring(0,sqlKey.length() -1); sqlValue = sqlValue.substring(0,sqlValue.length() -1); sql = sqlStr1 + sqlKey + sqlStr2 + sqlValue + sqlStr3; logger.info("拼接的insert into SQL:" + sql); Connection conn = getConnection(); conn.setAutoCommit(false); //构造预处理statement PreparedStatement pst = conn.prepareStatement(sql); int count = 0; int index = 1; for(int i = 1;i <= listMap.size();i++){ for (Object val: listMap.get(i-1).values() ) { pst.setString((index++),nullToNull(val)); } index = 1; pst.addBatch(); //每10000次提交一次 if(i % 10000 == 0){//可以设置不同的大小; ++count; pst.executeBatch(); conn.commit(); pst.clearBatch(); } } // 最后插入不足1w条的数据 pst.executeBatch(); conn.commit(); logger.info("批次提交次数:" + ++count); pst.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } }
public Connection getConnection() {//建立返回值为Connectiong的方法 Connection con = null;//声明Connection对象 try {//加载数据库驱动类 Class.forName(driver); System.out.println("数据库驱动加载成功"); } catch (ClassNotFoundException e) { e.printStackTrace(); } try {//通过访问数据库的URL获取数据库连接对象 con = DriverManager.getConnection(url, user, password); System.out.println("数据库连接成功"); } catch (SQLException e) { e.printStackTrace(); } return con;//按方法要求返回一个Connectiong对象 }
java向数据库批量插入数据
标签:red 不同的 out get otf for @Value commit 声明