博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
处理重复导入的方法之一
阅读量:5143 次
发布时间:2019-06-13

本文共 9761 字,大约阅读时间需要 32 分钟。

这是一个很简单的处理重复导入的方法,但是速度比较慢,我想除了这个方法之外应该也可以用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
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
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; }

 

转载于:https://www.cnblogs.com/xinxinjava/p/3237557.html

你可能感兴趣的文章
LeetCode #303. Range Sum Query
查看>>
oracle 自增序列实现 可作为主键
查看>>
STM32----stlink口
查看>>
Android viewpager切换到最后一页时,跳转至其他activity
查看>>
开启GD拓展
查看>>
JQUERY 大于
查看>>
ZooKeeper做独立server执行(上)
查看>>
《经济地理与企业兴衰》学习笔记
查看>>
正确 C# 未来的期望
查看>>
树状数组
查看>>
POJ 1328 Radar Installation(经典贪婪)
查看>>
Netty源代码学习——EventLoopGroup原理:NioEventLoopGroup分析
查看>>
IOS status bar
查看>>
void及void指针含义的深刻解析
查看>>
【UVA】434-Matty&#39;s Blocks
查看>>
五、宽度优先搜索(BFS)
查看>>
运行一个窗体直接最大化并把窗体右上角的最大化最小化置灰
查看>>
Android开发技术周报 Issue#80
查看>>
hadoop2.2.0+hive-0.10.0完全分布式安装方法
查看>>
WebForm——IIS服务器、开发方式和简单基础
查看>>