private static CurVO executeBatchNoBindSQL(String sql, List voList, Connection connect) throws Exception { CurVO resultVO; if (voList == null || voList.size() == 0) { resultVO = new CurVO(); resultVO.setBatchBesult(new int[0]); } Statement stm = null; try { stm = connect.createStatement(); for (int i = 0; i < voList.size(); i++) { // P.print(DBAssistantTools.GetJointSQL(sql, voList.get(i), // null, false)); /** ---------------------打印------------------------- */ P.printSQL(sql, voList.get(i)); /** ---------------------打印------------------------- */ // 添加SQL字符串 stm.addBatch(SqlTool.GetJointSQL(sql, voList.get(i), null, false)); } // 该数组可查看每次SQL处理的整形结果 resultVO = new CurVO(); resultVO.setBatchBesult(stm.executeBatch()); resultVO.setResult(stm.getUpdateCount()); return resultVO; } catch (Exception e) { throw e; } finally { DBTool.close(null, stm, null); } }
/** * 批量处理SQL * * @param sql * @param voList * @return * @throws Exception */ protected CurVO executeBatchSQL(String sql, List voList) throws Exception { if (voList.size() == 0) return new CurVO(); CurVO vo; if (!this.bind) vo = executeBatchNoBindSQL(sql, voList, this.connect); else vo = executeBatchBindSQL(sql, voList, this.connect); vo.setResult(vo.getBatchBesult().length); return vo; }
private static CurVO executeBatchBindSQL(String sql, List<Object> voList, Connection connect) throws Exception { if (voList == null || voList.size() == 0) return new CurVO(); PreparedStatement ps = null; CurVO resultVO; try { // 哪些变量需要绑定 List<String> fieldNameList = SqlTool.GetSQLBindFieldNameList(sql, voList.get(0).getClass()); // 获得批量处理的绑定变量SQL ps = connect.prepareStatement(SqlTool.GetBindSQL(sql)); // 绑定变量 for (Object vo : voList) { // 当前SQL要绑定的所有参数 List<Object> valueList = SqlTool.GetSQLFieldValueList(vo, fieldNameList); // 绑定 for (int i = 0; i < valueList.size(); i++) DBTool.BindJavaValueByJavaType(ps, valueList.get(i), i + 1, connect); /** ---------------------打印------------------------- */ P.printBindSQL(sql, vo); /** ---------------------打印------------------------- */ // 添加到批量处理 ps.addBatch(); } ps.executeBatch(); resultVO = new CurVO(); // 绑定变量只能获得总更新记录,无法这么处理.setBatchBesult(ps.executeBatch()); resultVO.setResult(ps.getUpdateCount()); return resultVO; } catch (Exception e) { throw e; } finally { DBTool.close(null, ps, null); } }