JDBC连接数据库之批量处理(4)

批量处理JDBC语句提高处理速度

1.当需要成批插入或者更新记录时。可以采用Java的批量更新的机制,这一机制允许多条语句一次性提交给数据库批量处理。通常情况下比单独提交处理更有效率

JDBC的批量处理语句有包括下面两个方法:

——addBatch(String):添加需要批量处理的SQL语句或是参数;

——executeBatch();执行批量处理语句;

2通常我们会遇到两种批量执行的SQL语句的情况:

-多条SQL语句的批量处理;

-一个SQL语句的批量传参;

	
	//--------------------------------
     public void testBatchWithStatement(){//7539
    	 Connection connection=null;
    	 Statement statement=null;
    	 String sql=null;
    	 try{
    		 connection=JDBCTools.getConnection();
    		 JDBCTools.beginTx(connection);
    		 
    		 statement=connection.createStatement();
    		 long begin=System.currentTimeMillis();
    		 for(int i=0;i<100000;i++){
    			 sql="insert into customers values("+(i+1)+", 'name_"+i+"', '2016-06-18')";
    			 statement.executeUpdate(sql);
    		 }
    		 long end=System.currentTimeMillis();
    		
    		 JDBCTools.commit(connection);
    		 System.out.println("Time: "+(end-begin));
    	 }catch(Exception e){
    		 e.printStackTrace();
    	 }finally{
    		 JDBCTools.releaseDB(null, statement, connection);
    	 }
     }
     public void testBatchWithPreparedStatement(){//6870
    	 Connection connection=null;
    	 PreparedStatement preparedStatement=null;
    	 String sql=null;
    	 try{
    		 connection=JDBCTools.getConnection();
    		 JDBCTools.beginTx(connection);
    		 sql="insert into customers values(?,?,?)";
    		 preparedStatement=connection.prepareStatement(sql);
    		 Date date=new Date(new java.util.Date().getTime());
    		 
    		 long begin=System.currentTimeMillis();
    		 for(int i=0;i<100000;i++){
                preparedStatement.setInt(1,i+1);
                preparedStatement.setString(2, "name_"+i);
                preparedStatement.setDate(3, date);
                preparedStatement.executeUpdate();
    		 }
    		 long end=System.currentTimeMillis();
       		 System.out.println("Time: "+(end-begin));
    		 JDBCTools.commit(connection);
 
    	 }catch(Exception e){
    		 e.printStackTrace();
    	 }finally{
    		 JDBCTools.releaseDB(null, preparedStatement, connection);
    	 }
     }
     //批量处理JDBC的
 	public void testBatch(){//6732
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		String sql = null;
		
		try {
			connection = JDBCTools.getConnection();
			JDBCTools.beginTx(connection);
			sql = "INSERT INTO customers VALUES(?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			Date date = new Date(new java.util.Date().getTime());
			
			long begin = System.currentTimeMillis();
			for(int i = 0; i < 100000; i++){
				preparedStatement.setInt(1, i + 1);
				preparedStatement.setString(2, "name_" + i);
				preparedStatement.setDate(3, date);
				
				//"积攒" SQL 
				preparedStatement.addBatch();
				
				//当 "积攒" 到一定程度, 就统一的执行一次. 并且清空先前 "积攒" 的 SQL
				if((i + 1) % 300 == 0){
					preparedStatement.executeBatch();
					preparedStatement.clearBatch();
				}
			}
			
			//若总条数不是批量数值的整数倍, 则还需要再额外的执行一次. 
			if(100000 % 300 != 0){
				preparedStatement.executeBatch();
				preparedStatement.clearBatch();
			}
			
			long end = System.currentTimeMillis();
			
			System.out.println("Time: " + (end - begin)); //569
			
			JDBCTools.commit(connection);
		} catch (Exception e) {
			e.printStackTrace();
			JDBCTools.rollback(connection);
		} finally{
			JDBCTools.releaseDB(null, preparedStatement, connection);
		}
	}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值