JDBC batch warehousing processing

Table creation statement:

CREATE TABLE `a` (
  `id` int(11) NOT NULL,
  `name` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
  `age` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin

Java code

public static void main(String[] args) {
        String jdbcUrl =
            "jdbc:mysql://127.0.0.1:3306/abc?useSSL=false&serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true";
        String username = "root";
        String passwd = "******!";
        String sql = "insert into a(id,name,age) values(?,?,?)";
        try (Connection con = DriverManager.getConnection(jdbcUrl, username, passwd);
            PreparedStatement ps = con.prepareStatement(sql);) {
            int j = 0;
            while (j++ < 10) {
                int i = 1;
                ps.setObject(i++, j);
                ps.setObject(i++, UUID.randomUUID().toString().replace("-", ""));
                ps.setObject(i++, "xxxxx");
                ps.addBatch();
            }
            ps.executeBatch();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

View Results

 

Guess you like

Origin blog.csdn.net/superfreeman/article/details/125058077