JDBC批处理:
public void save(){
String sql ="insert into mytem_xxx values(?)";
Connetct conn =DriveManager("url","userId","pwd");
PreparedStatement pre =null;
try{
conn.setAutoCommit(false); //禁止自动提交
pre=conn.preparedStatement(sql);//预处理
for(int i =1 ; i <=105 ; i++){
pre.setInt(1,i);
pre.addBatch(); //把sql语句加入批处理
if(i % 10 ==0){ //每10条处理一次,避免批处理中的sql语句太多
pre.executeBatch(); //执行
pre.clearBatch(); //清楚
}
}
pre.executeBatch(); //执行剩下的不足10条的sql
conn.commit();
}catch(SQLException e){
try{ conn.rollback(); } catch(SQLException e1){ e1.printStackTrace();}
e.printStackTrace();
}finally{
pre.close();
conn.close();
}
}