使用jdbc向數據庫插入100000條記錄,分別使用statement,PreparedStatement,及PreparedStatement+批處理3種方式進行測試:
1、使用statement插入100000條記錄
public void exec(Connection conn){
try { Long beginTime = System.currentTimeMillis(); conn.setAutoCommit(false);//設置手動提交 Statement st = conn.createStatement(); for(int i=0;i<100000;i++){ String sql="insert into t1(id) values ("+i+")"; st.executeUpdate(sql); } Long endTime = System.currentTimeMillis(); System.out.println("st:"+(endTime-beginTime)/1000+"秒");//計算時間 st.close(); conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } |
2、使用PreparedStatement對象
public void exec2(Connection conn){
try { Long beginTime = System.currentTimeMillis(); conn.setAutoCommit(false);//手動提交 PreparedStatement pst = conn.prepareStatement("insert into t1(id) values (?)"); for(int i=0;i<100000;i++){ pst.setInt(1, i); pst.execute(); } conn.commit(); Long endTime = System.currentTimeMillis(); System.out.println("pst:"+(endTime-beginTime)/1000+"秒");//計算時間
|