package com.hspedu.jdbc;
import org.junit.jupiter.api.Test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
/**
* @author: guorui fu
* @versiion: 1.0
*/
public class Batch {
@Test
public void batch() throws Exception {
Connection connection = JDBCUtils.getConnection();
String sql = "insert into admin values(?,?)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
long start = System.currentTimeMillis();
for (int i = 0; i < 5000; i++) {
preparedStatement.setString(1,"jak");
preparedStatement.setString(2,"abc");
//语句添加到批处理包里
preparedStatement.addBatch();
if (((i + 1) % 1000) == 0){
preparedStatement.executeBatch();
//到了装1000条执行后清空
preparedStatement.clearBatch();
}
}
long end = System.currentTimeMillis();
System.out.println("Batch方法执行时间" + (end - start));
JDBCUtils.close(null,connection,preparedStatement);
}
}
JDBC Batch批处理方法
最新推荐文章于 2024-05-11 16:18:46 发布