1821349743@qq.com
2023-02-20 93dd9bc3f16b0f626761ec624f2dc78037568897
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import org.junit.Test;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
 
/**
 * @Author cheng
 * @Date 2022/2/12 23:03
 * @Desc
 */
public class SyncDataTest {
 
    @Test
    public void addData() throws SQLException, ClassNotFoundException {
 
        int addDataNumber = 4961345+78;
        Connection connection = getConnection("jdbc:mysql://127.0.0.1:3306/test_sync?autoReconnect=true&useUnicode=true&characterEncoding=utf8&useNewIO=true&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true", "com.mysql.cj.jdbc.Driver", "root", "root123");
        PreparedStatement pst = connection.prepareStatement(
                " INSERT INTO `sync_data_test` " +
                        "(`source_name`, `desc`,`source_TIMESTAMP`,`update_time`) " +
                        "VALUES (concat('这是增加的数据--',?),concat('这是测试数据描述--',?),now(),null)");
        connection.setAutoCommit(false);
        for (int i = 4961345; i < addDataNumber; i++) {
            pst.setObject(1, i);
            pst.setObject(2, i);
            pst.addBatch();
            if (i != 0 && i % 5000 == 0) {
                pst.executeBatch();
                connection.commit();
                pst.clearBatch();
            }
        }
        pst.executeBatch();
        connection.commit();
        pst.close();
        connection.close();
    }
 
    public static Connection getConnection(String url, String driver, String user, String pwd) throws ClassNotFoundException, SQLException {
        Class.forName(driver);
        return DriverManager.getConnection(url, user, pwd);
    }
 
}