JDBC大批量写入数据到SQLServer2000,记录数大于10000

本文介绍了一种使用Java进行数据库批量插入的方法,通过控制每1000条数据执行一次批处理来提高插入效率,并记录了整个过程所用的时间。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

		SpObserver.putSp("sessionFactory1"); 
		SimpleDateFormat fomat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Session s=null;
		s=daoSupport.getSessionFactory().openSession();
		Connection con=s.connection();
		Statement stmt = null;
		try {
			stmt=con.createStatement();
			con.setAutoCommit(false);
			Iterator it=list.iterator();
			int iCount = 1;
			long start = System.currentTimeMillis();
			while(it.hasNext()){
				TResultWaterZId t = (TResultWaterZId)it.next();
				StringBuffer sb = new StringBuffer();
				sb.append("insert into T_result_water_z(schemeid,stcd,z,tm) values('");
				sb.append(t.getSchemeid()).append("','").append(t.getStcd()).append("','");
				sb.append(t.getZ()).append("','").append(fomat.format(t.getTm())).append("')");
				stmt.addBatch(sb.toString());
				if(iCount % 1000 == 0){
					stmt.executeBatch();
					stmt.clearBatch();
				}
				iCount++;
			}
			stmt.executeBatch();
			stmt.clearBatch();
			con.commit();
			long end = System.currentTimeMillis();
			System.out.println("addTResultWaterZId used time:"+(end-start));
			stmt.close();
			con.close();
		} catch (SQLException e) {
			try {
				con.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			try {
				stmt.close();
				con.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}

转载于:https://www.cnblogs.com/qumao5736/archive/2009/12/07/1618547.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值