我想使用jdbc在一段时间内添加记录数据库表。
例如,我希望以10秒的间隔添加100000条记录,因此它将插入10000 /秒。
我的MySQL代码如下:
String url1 = "jdbc:mysql://localhost:3306/xyz";
String user = "root";
String password = "root";
conn1 = DriverManager.getConnection(url1, user, password);
if (conn1 != null) {
System.out.println("Connected to the database xyz");
for(int i=0;i<=n;i++){ // where n is no. of record that I want to insert
// Here is my insert logic
}
}
答案 0 :(得分:3)
@ yogesh-jalodara在我的评论中,我的意思是那样的
final long loopDuration = 1;//second
final long totalSize = 100000;
final long timeInterval = 10;
final AtomicLong batchNumber = new AtomicLong((long)Math.ceil((double) timeInterval / loopDuration));
Timer timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
//insert logic
if (batchNumber.decrementAndGet() == 0) {
timer.cancel();
timer.purge();
}
}
}, 0, loopDuration * 1000);
答案 1 :(得分:2)
使用批量查询,您可以有效地减少数据库往返,节省了大量花在网络延迟上的时间,从而提高了Java应用程序的性能。
public class MySQLJdbcExample {
public static void main(String args[]) throws SQLException {
String url="jdbc:mysql://localhost:3306/test";
Connection conn = DriverManager.getConnection(url, "root", "root");
String query = "insert into employee values (?,?,NULL)";
PreparedStatement pStatement = conn.prepareStatement(query);
int batchSize = 100;
long startTime = System.currentTimeMillis();
for (int count = 0; count < 1000; count++) {
pStatement.setString(1, Integer.toString(count));
pStatement.setString(2, "Employee" + count);
pStatement.addBatch();
if (count % batchSize == 0) {
pStatement.executeBatch();
}
}
pStatement.executeBatch() ; //for remaining batch queries if total record is odd no.
// conn.commit();
pStatement.close();
conn.close();
long endTime = System.currentTimeMillis();
long elapsedTime = (endTime - startTime)/1000; //in seconds
System.out.println("Total time required to execute 1000 queries using PreparedStatement with JDBC batch insert is :" + elapsedTime);
}
}