Java在數據庫操作中批量插入數據

Class.forName("com.mysql.jdbc.Driver");  
Connection con
= (Connection) DriverManager.getConnection("jdbc:mysql://" +  
       
"localhost:3306/excel2mysql", "wanle", "wanle");  
// 關閉事務自動提交  
con.setAutoCommit(false);  
 
SimpleDateFormat sdf
= new SimpleDateFormat("HH:mm:ss:SS");  
TimeZone t
= sdf.getTimeZone();  
t.setRawOffset(
0);  
sdf.setTimeZone(t);  
Long startTime
= System.currentTimeMillis();  
 
PreparedStatement pst
= (PreparedStatement) con.prepareStatement("insert into test04 values (?,'中國')");  
for (int i = 0; i < 10000; i++) {  
    pst.setInt(
1, i);  
   
// 把一個SQL命令加入命令列表  
    pst.addBatch();  
}  
// 執行批量更新  
pst.executeBatch();  
// 語句執行完畢,提交本事務  
con.commit();  
 
Long endTime
= System.currentTimeMillis();  
System.out.println(
"用時:" + sdf.format(new Date(endTime - startTime)));  
 
pst.close();  
con.close(); 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章