日期:2014-05-16  浏览次数:20435 次

jdbc批量插入
try {
			conn.setAutoCommit(false);
			Long beginTime = System.currentTimeMillis();
			PreparedStatement pst = conn
					.prepareStatement("insert into t1(id) values (?)");

			for (int i = 1; i <= 1000000; i++) {
				pst.setInt(1, i);
				pst.addBatch();// 加入批处理,进行打包
				if (i % 1000 == 0) {// 可以设置不同的大小;如50,100,500,1000等等
					pst.executeBatch();
					conn.commit();
					pst.clearBatch();
					System.out.println("没死" + i);
				}
			}
			pst.executeBatch();
			conn.commit();
			Long endTime = System.currentTimeMillis();
			System.out.println("pst+batch用时:" + (endTime - beginTime) + "毫秒");
			pst.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}