- mysql批量導入功能測試
批量導入的優勢可以一次性提交事務,mysql底層有優化速度較快。
private static final int MAX = 10000;
@Test
public void test1() throws Exception {
Driver driver = null;
String driverClassName = "com.mysql.jdbc.Driver";
Class clazz = Class.forName(driverClassName);
driver = (Driver) clazz.newInstance();
String url = "jdbc:mysql://127.0.0.1:3306/sys?rewriteBatchedStatements=true";
Properties info = new Properties();
info.setProperty("user", "root");
info.setProperty("password", "root");
Connection conn = null;
try {
conn = driver.connect(url, info);
String sql = "INSERT INTO `test` (`id`,`name`) value(?,?)";
PreparedStatement PS = conn.prepareStatement(sql);
conn.setAutoCommit(false);
long staTime = System.currentTimeMillis();
for (int i = 0; i < MAX; i++) {
PS.setInt(1, i + 1);
PS.setString(2, String.valueOf(i));
PS.addBatch();
}
PS.executeBatch();
conn.commit();
long endTime = System.currentTimeMillis();
System.out.println((endTime - staTime));
} catch (Exception e) {
e.printStackTrace();
} finally {
conn.close();
}