如何避免从Excel向数据库插入相同的数据

时间:2017-05-12 11:00:34

标签: java oracle spring-boot apache-poi spring-data-jpa

我开发了一个rest API,它从excel中读取数据并将其插入到多个数据库中。现在我的问题是我想避免从excel插入重复数据。我可以建议如何在每次迭代插入一个对象时检查数据的重复

@RequestMapping(value = "/excel", method = RequestMethod.POST, consumes = javax.ws.rs.core.MediaType.MULTIPART_FORM_DATA)
public void uploadFileHandler(@RequestParam("organization") UUID organization,
                              @RequestParam("file") MultipartFile file) throws IOException {

    /*****GEt the organizaation id from form data and store to global variable*****/
    this.OrganizationID = organization;
    System.out.println("*****************************");

    /***Possible output parameters from file object***/

    /***Parameters required for S3 bucket upload***/
    String timeStamp = new SimpleDateFormat("yyyy.MM.dd.HH.mm.ss").format(new java.util.Date());
    InputStream is = file.getInputStream();
    String keyname = timeStamp+file.getOriginalFilename();
    String bucketname = "pharmerz-chat";

    /**Call S3 upload class and upload file**/
    amazons3.uploadfile(is, keyname, bucketname);
    /****Get organisation object from organisation id***/
    this.organization = iOrganizationRepository.findOne(organization);

    /**Excel File reader**/
    try {
        ExcelController ex = new ExcelController();
        File f1 = ex.convert(file);
        //     FileInputStream file = new FileInputStream(new File("E://Imp/Details.xlsx"));
        XSSFWorkbook workbook = new XSSFWorkbook(f1);
        XSSFSheet sheet = workbook.getSheetAt(0);
        Iterator<Row> rowIterator = sheet.iterator();
        rowIterator.next();

    // while (rowIterator.hasNext()) {
        for(int i=1;i<=51;i++){
            Row row = rowIterator.next();
            //For each row, iterate through each columns
            Iterator<Cell> cellIterator = row.cellIterator();

            while (cellIterator.hasNext()) {
                Cell cell = cellIterator.next();
                //        System.out.println("cell.getStringCellValue().length() "+cell.getStringCellValue().length());
                //   if( cell.getStringCellValue().length()>0){
                List list = new ArrayList();
                //       This will change all Cell Types to String
                cell.setCellType(Cell.CELL_TYPE_STRING);
                switch (cell.getCellType()) {
                    case Cell.CELL_TYPE_BOOLEAN:
                        String bstring = "" + cell.getBooleanCellValue();
                        list.add(cell.getBooleanCellValue());
                        System.out.println("boolean===>>>" + cell.getBooleanCellValue() + "\t");
                        break;
                    case Cell.CELL_TYPE_NUMERIC:
                        String Nstring = "" + cell.getNumericCellValue();
                        list.add(Nstring);
                        break;
                    case Cell.CELL_TYPE_STRING:
                        list.add(cell.getStringCellValue());
                        break;
                }
//                   }else {
//                       break;
                //        }
            }
            String productname = row.getCell(0).getStringCellValue();
            String composition = row.getCell(1).getStringCellValue();
            String productcategeory = row.getCell(2).getStringCellValue();
            String uniqueproductcode = row.getCell(3).getStringCellValue();
            String categeory = row.getCell(4).getStringCellValue();
            String note = row.getCell(5).getStringCellValue();

            //         System.out.println(productname+"" +composition+"" +productcategeory+" "+uniqueproductcode+" "+categeory+" "+note);
            System.out.println("productname " + productname);
            System.out.println("composition " + composition);
            System.out.println("productcategeory " + productcategeory);
            System.out.println("uniqueproductcode " + uniqueproductcode);
            System.out.println(" categeory " + categeory);
            System.out.println("note " + note);
            ExcelController ex1 = new ExcelController();
            //  ex1.InsertRowInDB3(name,email,add,company);

            System.out.println("");
            //     Category categeoryobject=iCategoryRepository.findOne(UUID.fromString(CategeoryId));
            Category categeoryobject = iCategoryRepository.findByCategory(productcategeory);

            if(composition.isEmpty()){
                composition=null;
            }
            if(uniqueproductcode.isEmpty()){
                uniqueproductcode=null;
            }
            if(note.isEmpty()){
                note=null;
            }

            Product product = new Product();
            product.setProduct(productname);
            product.setComposition(composition);
            product.setCategory(categeoryobject);
            product.setUpc(uniqueproductcode);
            product.setNote(note);

            insertProduct(product, categeory);
        }
        workbook.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (InvalidFormatException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
  //      e.printStackTrace();
        System.out.println("data completed upload");
    }
}

/**Insert product into database***/
public String insertProduct (Product product, String categeory) {

    try{System.out.println("getProduct " + product.getProduct());
        System.out.println("getComposition " + product.getComposition());
        System.out.println("getCategory " + product.getCategory());
        System.out.println("getUpc " + product.getUpc());
        System.out.println("getCategory " + product.getCategory());
        System.out.println("getNote " + product.getNote());

        if (product.getProduct().isEmpty()) {
            System.out.println("Product is empty");
            return "Product is empty";
        } else {
            Product savedproduct = iProductRepository.save(product);
            if (categeory.equalsIgnoreCase("Sale")) {
                Supplier supplier = new Supplier();
                supplier.setProduct(savedproduct);
                supplier.setOrganization(organization);
                iSupplierRepository.save(supplier);
                System.out.println("Product added to supplier " + categeory);
            } else if (categeory.equalsIgnoreCase("Purchase")) {
                Purchaser purchaser = new Purchaser();
                purchaser.setOrganization(organization);
                purchaser.setProduct(savedproduct);
                iPurchaserRepository.save(purchaser);
                System.out.println("Product added to purchaser " + categeory);
            } else {
                Supplier supplier = new Supplier();
                supplier.setProduct(savedproduct);
                supplier.setOrganization(organization);
                iSupplierRepository.save(supplier);
                Purchaser purchaser = new Purchaser();
                purchaser.setOrganization(organization);
                purchaser.setProduct(savedproduct);
                iPurchaserRepository.save(purchaser);
                System.out.println("Product added to both " + categeory);
            }

            System.out.println("product added");
            System.out.println("/************************");
            System.out.println(savedproduct.getId());
        }

        return "product added";}
        catch (Exception e){
      //  e.printStackTrace();
        return "all product finished";
        }
}

public File convert(MultipartFile file) throws IOException {
    File convFile = new File(file.getOriginalFilename());
    convFile.createNewFile();
    FileOutputStream fos = new FileOutputStream(convFile);
    fos.write(file.getBytes());
    fos.close();
    return convFile;
}

1 个答案:

答案 0 :(得分:1)

您需要阅读数据库设计并正确实施数据定义(例如,您的记录的主键是什么......)。如果没有正确的关系数据库设计,您就无法使用Oracle的声明性功能(即无需编写代码)(例如主键,......)。

话虽如此,如果您需要,可以通过识别复制并删除它们来删除复制品,例如:

select c1, c1, c3 /* ... cn */ , count(*), min(rowid), max(rowid)
from t
group by c1, c1, c3 /* ... cn */
having count(*) > 1
;

然后删除重复项,以便只剩下1个。