处理重复导入的方法之一
这是一个很简单的处理重复导入的方法,但是速度比较慢,我想除了这个方法之外应该也可以用sql语句来对他进行处理吧!
我这里是对导入数据库的每一条记录和数据库里面相关的所有记录进行比较,只要存在相同的记录就不导入,如果没有相同的就插入新的记录!
以下是我的代码:
//导入数据库 public String intoDB() throws IOException{ HttpServletRequest request = ServletActionContext.getRequest(); HttpSession session = request.getSession(); String path = ServletActionContext.getServletContext().getRealPath( "/general"); String fileName = null; while(fileName == null){ fileName = (String) session.getAttribute("fileName"); } File f = new File(path + "/" + fileName); try { //用jxl解析excel2003 if(this.getFileFileName()!=null && this.getFileFileName().indexOf(".xlsx")==-1){ Workbook book = Workbook.getWorkbook(f);// Sheet sheet = book.getSheet(0); // 获得第一个工作表对象 for (int i = 1; i < sheet.getRows(); i++) { BoatTable boat = new BoatTable(); boat.setBenglishname(sheet.getCell(0, i).getContents()); boat.setBchinesename(sheet.getCell(1, i).getContents()); boat.setBregist(sheet.getCell(2, i).getContents()); boat.setBboat(sheet.getCell(3, i).getContents()); boat.setBrecongnize(sheet.getCell(4, i).getContents()); boat.setBmaterial(sheet.getCell(5, i).getContents()); boat.setBlength(Double.parseDouble(sheet.getCell(6, i).getContents())); boat.setBwidth(Double.parseDouble(sheet.getCell(7, i).getContents())); boat.setBdeep(Double.parseDouble(sheet.getCell(8, i).getContents())); boat.setBsum(Integer.parseInt(sheet.getCell(9, i).getContents())); boat.setBsulttle(Integer.parseInt(sheet.getCell(10, i).getContents())); boat.setBgross(Integer.parseInt(sheet.getCell(11, i).getContents())); boat.setBpower(Integer.parseInt(sheet.getCell(12, i).getContents())); boat.setBlog(sheet.getCell(13, i).getContents()); boat.setBremark(sheet.getCell(14, i).getContents()); boat.setBcheck(sheet.getCell(15, i).getContents()); boat.setBwater(sheet.getCell(16, i).getContents()); boat.setBoperat(sheet.getCell(17, i).getContents()); boat.setBrange(sheet.getCell(18, i).getContents()); boat.setBkind(sheet.getCell(19, i).getContents()); boat.setBowner(sheet.getCell(20, i).getContents()); //添加到数据库,处理重复提交 List<BoatTable> bList=boatService.propertyBoat("bchinesename", boat.getBchinesename()); boolean flag = false; for (BoatTable blist : bList) { if(!blist.getBboat().equals(boat.getBboat()) || !blist.getBcheck().equals(boat.getBcheck()) || !blist.getBchinesename().equals(boat.getBchinesename()) || !blist.getBdeep().equals(boat.getBdeep()) || !blist.getBenglishname().equals(boat.getBenglishname()) || !blist.getBgross().equals(boat.getBgross()) || !blist.getBkind().equals(boat.getBkind()) || !blist.getBlength().equals(boat.getBlength()) || !blist.getBlog().equals(boat.getBlog()) || !blist.getBmaterial().equals(boat.getBmaterial()) || !blist.getBoperat().equals(boat.getBoperat()) || !blist.getBowner().equals(boat.getBowner()) || !blist.getBpower().equals(boat.getBpower()) || !blist.getBrange().equals(boat.getBrange()) || !blist.getBrecongnize().equals(boat.getBrecongnize()) || !blist.getBregist().equals(boat.getBregist()) || !blist.getBremark().equals(boat.getBremark()) || !blist.getBsulttle().equals(boat.getBsulttle()) || !blist.getBsum().equals(boat.getBsum()) || !blist.getBwater().equals(boat.getBwater()) || !blist.getBwidth().equals(boat.getBwidth())){ flag = false; }else{ flag = true; break; } } if(!flag){ boatService.addOneBoat(boat); } } }else{ //用poi解析excel2007 // 构建1个工作簿对象 XSSFWorkbook xwb = null; try { xwb = new XSSFWorkbook(new FileInputStream(f.getPath())); } catch (Exception e) { e.printStackTrace(); } //得到第一张工作表 XSSFSheet xSheet = xwb.getSheetAt(0); //循环工作表的每一行 for (int rowNum = 1; rowNum <= xSheet.getLastRowNum(); rowNum++) { XSSFRow xRow = xSheet.getRow(rowNum); if (xRow == null) { continue; } BoatTable boat = new BoatTable(); boat.setBenglishname(xRow.getCell(0).getStringCellValue()); boat.setBchinesename(xRow.getCell(1).getStringCellValue()); boat.setBregist(xRow.getCell(2).getStringCellValue()); if(xRow.getCell(3).getCellType() == XSSFCell.CELL_TYPE_NUMERIC){ boat.setBboat(Double.valueOf(xRow.getCell(3).getNumericCellValue()).toString()); }else{ boat.setBboat(xRow.getCell(3).getStringCellValue()); } boat.setBrecongnize(xRow.getCell(4).getStringCellValue()); boat.setBmaterial(xRow.getCell(5).getStringCellValue()); boat.setBlength(xRow.getCell(6).getNumericCellValue()); boat.setBwidth(xRow.getCell(7).getNumericCellValue()); boat.setBdeep(xRow.getCell(8).getNumericCellValue()); boat.setBsum((int)xRow.getCell(9).getNumericCellValue()); boat.setBsulttle((int)xRow.getCell(10).getNumericCellValue()); boat.setBgross((int)xRow.getCell(11).getNumericCellValue()); boat.setBpower((int)xRow.getCell(12).getNumericCellValue()); boat.setBlog(xRow.getCell(13).getStringCellValue()); boat.setBremark(xRow.getCell(14).getStringCellValue()); //如果单元格里面为数字,用String取得话会报异常,故有可能为数字的都要判断一下 if(xRow.getCell(15).getCellType() == XSSFCell.CELL_TYPE_NUMERIC){ boat.setBcheck(Double.valueOf(xRow.getCell(15).getNumericCellValue()).toString()); }else{ boat.setBcheck(xRow.getCell(15).getStringCellValue()); } if(xRow.getCell(16).getCellType() == XSSFCell.CELL_TYPE_NUMERIC){ boat.setBwater(Double.valueOf(xRow.getCell(16).getNumericCellValue()).toString()); }else{ boat.setBwater(xRow.getCell(16).getStringCellValue()); } if(xRow.getCell(17).getCellType() == XSSFCell.CELL_TYPE_NUMERIC){ boat.setBoperat(Double.valueOf(xRow.getCell(17).getNumericCellValue()).toString()); }else{ boat.setBoperat(xRow.getCell(17).getStringCellValue()); } boat.setBrange(xRow.getCell(18).getStringCellValue()); boat.setBkind(xRow.getCell(19).getStringCellValue()); boat.setBowner(xRow.getCell(20).getStringCellValue()); //添加到数据库 List<BoatTable> bList=boatService.propertyBoat("bchinesename", boat.getBchinesename()); boolean flag = false; for (BoatTable blist : bList) { if(!blist.getBboat().equals(boat.getBboat()) || !blist.getBcheck().equals(boat.getBcheck()) || !blist.getBchinesename().equals(boat.getBchinesename()) || !blist.getBdeep().equals(boat.getBdeep()) || !blist.getBenglishname().equals(boat.getBenglishname()) || !blist.getBgross().equals(boat.getBgross()) || !blist.getBkind().equals(boat.getBkind()) || !blist.getBlength().equals(boat.getBlength()) || !blist.getBlog().equals(boat.getBlog()) || !blist.getBmaterial().equals(boat.getBmaterial()) || !blist.getBoperat().equals(boat.getBoperat()) || !blist.getBowner().equals(boat.getBowner()) || !blist.getBpower().equals(boat.getBpower()) || !blist.getBrange().equals(boat.getBrange()) || !blist.getBrecongnize().equals(boat.getBrecongnize()) || !blist.getBregist().equals(boat.getBregist()) || !blist.getBremark().equals(boat.getBremark()) || !blist.getBsulttle().equals(boat.getBsulttle()) || !blist.getBsum().equals(boat.getBsum()) || !blist.getBwater().equals(boat.getBwater()) || !blist.getBwidth().equals(boat.getBwidth())){ flag = false; }else{ flag = true; break; } } if(!flag){ boatService.addOneBoat(boat); } } } } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } f.delete(); session.removeAttribute("fileName"); return null; }