更新时间:2024-11-26 GMT+08:00
插入数据
功能介绍
如下示例代码通过循环batchNum次,构造示例数据并通过PreparedStatement的executeBatch()方法批量插入数据。
其中数据类型为创建的表所指定的三个字段,分别是String、UInt8和Date类型。
样例代码
String insertSql = "insert into " + databaseName + "." + tableName + " values (?,?,?)"; PreparedStatement preparedStatement = connection.prepareStatement(insertSql); long allBatchBegin = System.currentTimeMillis(); for (int j = 0; j < batchNum; j++) { for (int i = 0; i < batchRows; i++) { preparedStatement.setString(1, "xxx_" + (i + j * 10)); preparedStatement.setInt(2, ((int) (Math.random() * 100))); preparedStatement.setDate(3, generateRandomDate("2018-01-01", "2021-12-31")); preparedStatement.addBatch(); } long begin = System.currentTimeMillis(); preparedStatement.executeBatch(); long end = System.currentTimeMillis(); log.info("Inert batch time is {} ms", end - begin); } long allBatchEnd = System.currentTimeMillis(); log.info("Inert all batch time is {} ms", allBatchEnd - allBatchBegin);
父主题: 样例代码说明