项目中,QueryRunner 的使用

项目中,有个需求,需要更新多条数据;

一开始,我是一条一条更新操作数据库表的,但是,这种效率比较差;

就想起来有没有批量处理更新数据的; 需要一个jar包:commons-dbutils-1.6.jar

代码如下:

// 更新客户 对应表:
	public static void updateCustDatas(Object obj,List<Cust> custLists) throws Exception {
		//定义个id集合
		List<String> idLists = new ArrayList<String>();
		for (Cust cust : custLists) {
			idLists.add(cust.getBasdocid());
		}
		
		Object[] idArr = new Object[idLists.size()];
		idLists.toArray(idArr);
		QueryRunner queryRunner = new QueryRunner(true);
		Object[][] params = new Object[idArr.length][1];
		for (int i = 0; i < idArr.length; i++) {
			params[i][0] = idArr[i];
		}
		
		conn = JdbcConnUtil.getJdbcConnect(obj);
		//下面sql中的*应该写成具体的字段,由于这个表字段太多,就省去了!
		String sql = "update fa_base_cust t set t.gxmlflag = to_char(sysdate,'yyyymmdd'),t.gxmldate=sysdate "
				+ "where t.basdocid = ? and t.gxmlflag is null ";
		
		int result = 0;
		try {
			result = queryRunner.batch(conn, sql, params).length;
			if(result>0) {
				System.out.println("数据更新成功");
			}else {
				System.out.println("数据更新失败");
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally{
			conn.close();
		}
		
	}

猜你喜欢

转载自blog.csdn.net/u013456370/article/details/81564291