当前位置:编程学习 > JAVA >>

采用jdbc批处理 提高jdbc效率 .

1.将jdbc操作改成批处理  addBatch(); //添加批处理

 

2.使用PreparedStatement

 

 

代码:

eg:

 

 

[java]
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); 
 
}  1.将jdbc操作改成批处理  addBatch(); //添加批处理

 

2.使用PreparedStatement

 

 

代码:

eg:

 

 

[java] 
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 { 
 www.zzzyk.com
     conn.setAutocommit(true); 
 


作者;tbwshc
补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,