- url?rewriteBatchedStatements=true
- insert 语句必须使用 values
- 语句后面不能添加分号
- 语句不能直接执行,每次需要装货 addBatch() 最后 executeBatch()
public static void main(String[] args) throws Exception{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql:///atguigu?rewriteBatchedStatements=true","root","123456");
String sql=" insert into t_user(account,password,nickname) values(?,?,?) ";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
long start = System.currentTimeMillis();
int InsertNum=10000;
for (int i = 0; i < InsertNum; i++) {
preparedStatement.setObject(1,"num"+i);
preparedStatement.setObject(2,i);
preparedStatement.setObject(3,"员工"+i);
preparedStatement.addBatch();
}
int[] ints = preparedStatement.executeBatch();
long end = System.currentTimeMillis();
System.out.println("消耗时间: "+(end-start));
if (ints.length==InsertNum) {
System.out.println("插入数据成功");
}else {
System.out.println("插入数据失败");
}
preparedStatement.close();
connection.close();
}