java 中oracle 更新批处理

与常规的jdbc更新处理只多了一个设置批值的步聚,相关代码如下:
    import java.sql.*;
import oracle.jdbc.*;
public class javabatch {

    
public static final int TOTAL_NUM_ROWS=2000;
    
public static final int BATCH_SIZE=10;
    
    
public static void main (String [] args) throws SQLException
    
{
        DriverManager.registerDriver(
new OracleDriver());
        Connection myconnection 
=DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:progather",
        
"username",
        
"passord");
        
        myconnection.setAutoCommit(
false);
        Statement mystatement
=myconnection.createStatement();
        mystatement.execute(
"truncate table tmp_cxf_1");
        
        System.out.println(
"Insert into table tmp_cxf_1 "+TOTAL_NUM_ROWS+" without batching");
        insertrows(myconnection,
false);
        System.out.println(
"Insert into tmp_cxf_1 with update batching");
        insertrows(myconnection,
true);
        mystatement.close();
        myconnection.close();
            
    }
//end of main
    
    
public static void insertrows(Connection myconnection,boolean batching) throws SQLException
    
{
        OraclePreparedStatement myoracleprepastatment 
=(OraclePreparedStatement)myconnection.prepareStatement("insert into tmp_cxf_1 (ind_code,sec_code) values (?,?)");
        
if (batching)
        
{
            System.out.println(
"setting the batch value to "+BATCH_SIZE);
            myoracleprepastatment.setExecuteBatch(BATCH_SIZE);
        }

        System.out.println(
"batch value="+myoracleprepastatment.getExecuteBatch());
        
long start_time=System.currentTimeMillis();
        
for (int count=0;count<TOTAL_NUM_ROWS;count++)
        
{
            myoracleprepastatment.setInt(
1, count);
            myoracleprepastatment.setInt(
2, count+100);
            
int rowsInserted=myoracleprepastatment.executeUpdate();
        }

        
long end_time=System.currentTimeMillis();
        System.out.println(
"Total time for inserting "+TOTAL_NUM_ROWS+"rows was "+(end_time-start_time)+" millsceconds");
        myconnection.rollback();
        myoracleprepastatment.close();
                    
    }
//insertrows
}


posted on 2012-04-11 22:48 snowhill 阅读(826) 评论(0)  编辑 收藏 引用 所属分类: java


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2024年8月>
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567

导航

公告

又一年...........

留言簿(3)

随笔分类(13)

文章分类(131)

文章档案(124)

c++

java

linux

oracle

常用软件

其他

网络配置

系统安全

音乐

搜索

最新评论

阅读排行榜