Java拼接批量新增SQL语句
StringBuffer addSql = new StringBuffer(1000); int batchSize = 50; int executeTime = 0; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); for (int i=0; i<trackIds.size(); i++) { if(i/batchSize > executeTime) {//50轮执行一次SQL(拷贝数据量太大) String sql = addSql.substring(0, addSql.length() - 1); baseJdbcDao.exceute(sql, null); executeTime++;//执行次数++ } if(i%batchSize == 0) {//50轮重新拼接SQL(防止SQL过长) addSql = addSql.delete(0, addSql.length()); addSql.append("insert into " + DatabaseConstants.BASE + ".yk_behavior_monitor (TRACK_ID, PROGRAM_ID, " + "BEHAVIOR_ID, ACTION_TOTAL, ACTION_COMPLETED, CREATED_BY, CREATED_DATE, LAST_UPDATED_BY, LAST_UPDATED_DATE, REMOVE_FLAG) values"); } for (Integer behaviorId : behaviorIds) { addSql.append("("); addSql.append(trackIds.get(i) + ","); addSql.append(programId + ","); addSql.append(behaviorId + ","); addSql.append("0,0,"); addSql.append(user.getUserId() + ","); addSql.append("'" + sdf.format(new Date()) + "',"); addSql.append(user.getUserId() + ","); addSql.append("'" + sdf.format(new Date()) + "',"); addSql.append("0),"); } } String sql = addSql.substring(0, addSql.length() - 1); baseJdbcDao.exceute(sql, null);