java sql批量提交

使用PreparedStatement批量提交SQL。
Connection conn = null;
PreparedStatement pstmt 
= null;
try {
    Class.forName(
"oracle.jdbc.driver.OracleDriver");
    conn 
= DriverManager.getConnection(
                    
"jdbc:oracle:thin:@localhost:1521:orcl""scott",
                    
"tigter");
    String sql 
= "delete from table_tmp where id = ?";
    conn.setAutoCommit(
false);
    pstmt 
=   conn.prepareStatement(sql);
    
for(int i = 100; i<200; i++{
        pstmt.setString(
1,i);
        pstmt.addBatch();
    }

    pstmt.executeBatch();
    conn.commite();
}
 catch(Exception e) {
    e.printStackTrace();
}

finally {
 
try {
    
if(pstmt != null)
                    pstmt.close();
            }
 catch (SQLException e) {
                e.printStackTrace();
            }

            
            
try {
                
if(conn != null{
                    conn.close();
                }

            }
  catch (SQLException e) {
                e.printStackTrace();
            }
}

finally

批量处理时效率比较高,只进行一次数据库连接。

posted on 2011-10-20 11:48 hxwhui 阅读(2810) 评论(2)  编辑  收藏

评论

# re: java sql批量提交 2012-04-17 09:32 mqyl1


update t_a set name = ? where id = ?
update t_a set age = ? where id = ?

这两个可以进行批量提交吗?  回复  更多评论   

# re: java sql批量提交[未登录] 2012-04-17 09:36 hxwhui

@mqyl1
应该可以的 创建两个 PreparedStatement 应该就可以,提交是通过Connection的对象进行提交的,只要两个PreParedStatement是一个Connection的对象创建的就行。  回复  更多评论   


只有注册用户登录后才能发表评论。


网站导航:
 
<2012年4月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

导航

统计

常用链接

留言簿

随笔档案

搜索

最新评论

阅读排行榜

评论排行榜