Socket批量插入

/**
 * @author hrui
 * @date 2023/4/21 4:25
 */
public class InsertData {
    private static final int BATCH_SIZE = 10000;
    private static final int TOTAL_ROWS = 1000000;
    private static final String URL = "";
    private static final String USER = "";
    private static final String PASSWORD = "";
    public static void main(String[] args) {
        List list=InsertData.getPersonWithArg();
        System.out.println(list.size());
        for(Person p:list){
            System.out.println(p);
        }


        try (Connection connection = DriverManager.getConnection(URL, USER, PASSWORD)) {
            connection.setAutoCommit(false);
            StringBuilder sqlBuilder = new StringBuilder("INSERT INTO person (name,age,gender,qq) VALUES ");
            try (PreparedStatement statement = connection.prepareStatement(sqlBuilder.toString())) {
                for (int i = 1; i <= TOTAL_ROWS; i++) {
                    sqlBuilder.append("('" + list.get(i-1).getName()+"','"+list.get(i-1).getAge() + "','"+list.get(i-1).getGender() + "','"+list.get(i-1).getQq()+ "'),");
                    if (i % BATCH_SIZE == 0) {
                        sqlBuilder.deleteCharAt(sqlBuilder.length() - 1);
                        statement.addBatch(sqlBuilder.toString());
                        statement.executeBatch();/没必要clear 分号可以不加
                        connection.commit();
                        System.out.println("提交"+i/BATCH_SIZE+"次");
                        sqlBuilder = new StringBuilder("INSERT INTO person (name,age,gender,qq) VALUES ");
                    }
                }
                if (TOTAL_ROWS % BATCH_SIZE != 0) {
                    sqlBuilder.deleteCharAt(sqlBuilder.length() - 1);
                    statement.addBatch(sqlBuilder.toString());
                    statement.executeBatch();/
                    connection.commit();
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }


    // 模拟数据
    public static List getPersonWithArg(){
        List list=new ArrayList<>();
        //由一个StringBuffer来代替之前4个String的反复字符串操作。
        //StringBuffer sb = new StringBuffer("");// String = "";
        StringBuilder sb=new StringBuilder("");
        for (int r = 0; r < 1000000 ; r++) {
            Person person = new Person();
            //随机产生姓名
            for (int i = 0; i < 3; i++) {
                sb.append((char) (0x4e00 + (Math.random() * (0x9fa5 - 0x4e00 + 1))));
            }
            person.setName(sb.toString());
            //用完后清楚里面的内容,以供下一次使用
            sb.delete(0,sb.length());

            //随机产生性别
            int sexf = (int)(Math.random() * 2);//[0,2)
            if(sexf==0){
                sb.append("男");
            }else if( sexf == 1){
                sb.append("女");
            }else {
                sb.append("Unknow");
            }
            person.setGender(sb.toString());
            sb.delete(0,sb.length());
            //随机产生年龄
            int d;
            for (int i = 0; i < 2; i++) {
                d = (int)(Math.random()*10);
                sb.append(d);
            }
            person.setAge(sb.toString());
            sb.delete(0,sb.length());

            // 随机产生qq
            int j;
            for (int i = 0; i < 10; i++) {
                d = (int)(Math.random()*10);
                sb.append(d);
            }
            person.setQq(sb.toString());
            sb.delete(0,sb.length());

            list.add(person);
        }
        return list;
    }

}
class Person {
    private String id;
    private String name;
    private String age;
    private String qq;
    private String gender;

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAge() {
        return age;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getQq() {
        return qq;
    }

    public void setQq(String qq) {
        this.qq = qq;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Person person = (Person) o;
        return Objects.equals(id, person.id) && Objects.equals(name, person.name) && Objects.equals(age, person.age) && Objects.equals(qq, person.qq) && Objects.equals(gender, person.gender);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id, name, age, qq, gender);
    }

    @Override
    public String toString() {
        return "Person{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", age='" + age + '\'' +
                ", qq='" + qq + '\'' +
                ", gender='" + gender + '\'' +
                '}';
    }
}

你可能感兴趣的:(java,mysql,jvm)