通过MySQL批处理来提高效率
@Test
public void test01() throws SQLException {
//使用批处理
/*
告诉mysqL服务器开启批处理的支持
?rewriteBatchedstatements=true
addBatch();将任务赞到一起
excuteBatch();执行批处理
*/
String url="jdbc:mysql://localhost:3306/kanfang";
String user = "root";
String password="123456";
DriverManager.registerDriver(new Driver());
Connection connection = DriverManager.getConnection(url, user, password);
String sql="insert into batch values(null,?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
long startTime = System.currentTimeMillis();
for (int i = 0; i < 2000; i++) {
preparedStatement.setObject(1,"李白"+i);
preparedStatement.setObject(2,"password"+i);
//将数据攒到一起sele
preparedStatement.addBatch();
}
//将攒到一起的数据提交
preparedStatement.executeBatch();
long endTime = System.currentTimeMillis();
System.out.println("耗时:"+(endTime - startTime));
preparedStatement.close();
connection.close();
}