采用jdbc批處理 提高jdbc效率 .
1.將jdbc操作改成批處理 addBatch(); //添加批處理
2.使用PreparedStatement
代碼:
eg:
- Connection conn = DBUtils.getInstance().getConnetion();
- conn.setAutoCommit(false );
- PreparedStatement pstmt = null;
- try
- pstmt = conn.preparedStatement("insert into test1(a,b) vlaues (?,?)");
- pstmt.clearBatch();
- for(int i = 0; i<100000;i++){
- pstmt.setInt(1,i);
- pstmt.setString(2,"value"+i);
- pstmt.addBatch();
- if(i % 10000){
- pstmt.executbeBatch();
- }
- }
- pstmt.executeBatch();
- conn.commit();
- } catch(Exception e) {
- conn.rollback();
- } finally {
- conn.setAutocommit(true);
- }