配置文件中url后加?rewriteBatchedStatements=true
@Test
public void test01() {
Connection connection = JDBCUtils.getConnection();
String sql = "insert into actor values(null ,? ,?)";
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
System.out.println("开始!");
long start = System.currentTimeMillis();
for (int i = 0; i < 500; i++) {
//preparedStatement.setInt(1, i+1);
preparedStatement.setString(1, "李" + 1);
preparedStatement.setInt(2, 777);
preparedStatement.executeUpdate();
}
long end = System.currentTimeMillis();
System.out.println("传统方式耗时:" + (end - start));//传统方式耗时:23866
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(null, preparedStatement, connection);
}
}
//批处理新增500条
@Test
public void test02() {
Connection connection = JDBCUtils.getConnection();
String sql = "insert into actor values(null ,? ,?)";
PreparedStatement preparedStatement = null;
try {
preparedStatement = connection.prepareStatement(sql);
System.out.println("开始!");
long start = System.currentTimeMillis();
for (int i = 0; i < 500; i++) {
preparedStatement.setString(1, "李" + 1);
preparedStatement.setInt(2, 777);
preparedStatement.addBatch();
if ((i + 1) % 100 == 0) {
preparedStatement.executeBatch();
preparedStatement.clearBatch();
}
}
long end = System.currentTimeMillis();
System.out.println("传统方式耗时:" + (end - start));//传统方式耗时:361
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.close(null, preparedStatement, connection);
}
}



