采用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.executeBatch(); } } pstmt.executeBatch(); conn.commit(); } catch(Exception e) { conn.rollback(); } finally { conn.setAutocommit(true); }

 

你可能感兴趣的:(java,jdbc,exception,insert)