0

I need to compare excel data (rowwise) with database table.

Can someone please help me on that.

I can read excel file rowwise and column wise properly with below code

public class POC {

public static void main(String[] args) {

    String colText = null;
    XSSFWorkbook workbook;
    Cell cell = null;
    try {
        HashSet<String> xlRead = new HashSet<String>();

        InputStream myxls = new FileInputStream(new File(
                "excelpath.xlsx"));

        workbook = new XSSFWorkbook(myxls);

        Sheet sheet = workbook.getSheetAt(0); // Get Your Sheet.
        java.util.Iterator<Row> rowterator = sheet.rowIterator();
        while (rowterator.hasNext()) {
            Row row = rowterator.next();
            java.util.Iterator<Cell> cellIterator = row.cellIterator();
            while (cellIterator.hasNext()) {
                cell = cellIterator.next();
                switch (cell.getCellType()) {
                case Cell.CELL_TYPE_STRING:
                    xlRead.add(cell.getStringCellValue() + "");
                    System.out.println(cell.getStringCellValue());

                default:
                    break;
                }
            }
        }
        System.out.println("");
        myxls.close();

        for (String s : xlRead)
            System.out.println(s);
    } catch (IOException e) {
        e.printStackTrace();
       }
   }
}
Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
Dolly
  • 97
  • 3
  • 18

1 Answers1

0

Would suggest you just create an object to store the your data from the database (use a JPA for this) with an customer implementation of compareTo(Object object). You can then use the same object to store the data you getting from excel and compare the two objects using the 'compareTo' method you created?

@Entity 
@Table(name="Age") 
public class MyAge() {

    public MyAge();

    @Id
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    int id;

    @Column(name="age")
    private int age;

    ... getters and setters, though lombok is better ... 

    @Override
    public int compareTo(MyAge myAge) {
      return Integer.compare(age, myAge.age);
    }
}
Ambro-r
  • 919
  • 1
  • 4
  • 14