【发布时间】:2020-09-22 11:27:54
【问题描述】:
我使用 jdbc、mysql-connector-java-8.0.20 和 xampp 作为 mysql 服务器在数据库中插入 5000 条记录 我使用this link 进行测试,这是我测试的代码
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.util.Random;
public class test {
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/sonoo", "root", "");
// here sonoo is database name, root is username and password
String sql = "insert into emp(name, age) values(?, ?)";
long startTime = System.currentTimeMillis();
for (int i = 0; i < 5000; i++) {
PreparedStatement ps = con.prepareStatement(sql);
Random rnd = new Random();
int age = rnd.nextInt(60);
byte [] name = new byte[30];
rnd.nextBytes(name);
ps.setString(1, name.toString());
ps.setInt(2, age);
ps.executeUpdate();
System.out.println(i);
}
long endTime = System.currentTimeMillis();
System.out.println("taken time: " + (endTime - startTime));
con.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
输出是:
花费时间:315309
【问题讨论】:
-
将
PreparedStatement ps = con.prepareStatement(sql);移到for之外。