56,679
社区成员
发帖
与我相关
我的任务
分享
package com.ray.test.db;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class App {
public static void main(String[] argv) {
if (!testClass())
return;
try {
String url = "jdbc:mysql://localhost:3306/test?user=root&password=220315";
Connection conn = DriverManager.getConnection(url);
if (!conn.isClosed())
System.out.println("資料庫連線成功");
// java.sql.PreparedStatement caller =
// conn.prepareStatement("insert into test values (?)");
// System.out.println("start excute : " +
// System.currentTimeMillis());
// for (int i = 0; i < 10000; i++) {
// caller.setInt(1, i);
// caller.executeUpdate();
// }
// System.out.println("finish excute : " +
// System.currentTimeMillis());
java.sql.PreparedStatement caller = conn.prepareStatement("insert into test values (?)");
System.out.println("start excute : " + System.currentTimeMillis());
for (int i = 0; i < 10000; i++) {
caller.setInt(1, i);
caller.addBatch();
}
caller.executeBatch();
System.out.println("finish excute : " + System.currentTimeMillis());
conn.close();
} catch (SQLException e) {
System.out.println("Exception occur, e.getMessage()=" + e.getMessage());
}
}
private static boolean testClass() {
try {
Class.forName("com.mysql.jdbc.Driver");
return true;
} catch (ClassNotFoundException e) {
System.out.println("找不到驅動程式類別, e.getMessage()=" + e.getMessage());
}
return false;
}
}
System.out.println("start excute: \t" + System.currentTimeMillis());
StringBuilder builder = new StringBuilder("INSERT INTO test VALUES ");
for (int i = 0; i < 10000; i++) {
if (i == 9999)
builder.append("(9999)");
else
builder.append("( " + i + " ), ");
}
Statement sm = conn.createStatement();
sm.execute(builder.toString());
System.out.println("finish excute: \t" + System.currentTimeMillis());