JavaFX Apache POI Excel Login Program

Yiue :

I am new to Java and I've been studying it for a while now

I want to make a login program using excel to store the information.

The problem is that I don't know how to code the log in button. I searched and came up with this code using iterator to find the values inside the XSSFSheet.

Can someone help me? Greatest Gratitude for those who will answer.

public void logInButtonPressed() {
    try {
        String user = userLogTF.getText();
        String pass = passLogPF.getText();

        FileInputStream inputStream = new FileInputStream(new File("Database.xlsx"));
        Workbook workbook = WorkbookFactory.create(inputStream);
        Sheet sheet = workbook.getSheetAt(0);
        int i = 0;
        boolean found = false;

        Iterator<Row> rows = sheet.iterator();

        while (rows.hasNext() || !found) {
            XSSFRow row = (XSSFRow) rows.next();
            XSSFCell cell = row.getCell(i++);
            String userName = cell.toString();
            Iterator<Cell> cellIterator = row.iterator();
            if (userName.equals(user)) {
                found = true;

            } else {
                i++;
            }

            while (cellIterator.hasNext()) {
                Cell passCell = cellIterator.next();
                String passWord = passCell.toString();
                if (passWord.equals(pass)) {
                    JOptionPane.showMessageDialog(null, "Logged In");
                    break;
                } else {
                    JOptionPane.showMessageDialog(null, "Invalid Input");
                    break;
                }

            }

        }
    } catch (Exception e) {
    }
}

This is the excel file

enter image description here

This is the Gui

enter image description here

deHaar :

You don't really need an iterator if you know the columns for username and password. You can simply iterate by row index and directly access the columns by indexes 1 and 2.
View the following example and carefully read the code comments:

public void logInButtonPressed() {
    String user = userLogTF.getText();
    String pass = passLogPF.getText();
    // provide some flags for the items to be found and matched
    boolean foundUser = false;
    boolean passwordMatchesUser = false;

    try (FileInputStream fis = new FileInputStream(new File("Database.xlsx"))) {
        Workbook workbook = new XSSFWorkbook(fis);
        // there is only one sheet in your workbook, so take the first one
        Sheet sheet = workbook.getSheetAt(0);

        // the values start at row 1 (on a zero-based index), first row contains headers
        for (int r = 1; r < sheet.getPhysicalNumberOfRows(); r++) {
            Row row = sheet.getRow(r);
            // you know the users are stored in column 2 (or B), again: zero-based index
            Cell userCell = row.getCell(1);

            // check for a match in the user cells
            if (userCell.toString().equals(username)) {
                // set the flag to true if the user was found
                foundUser = true;
                // then directly switch to the password cell in the next column
                Cell passwordCell = row.getCell(2);

                // and check if the password is correct
                if (passwordCell.toString().equals(password)) {
                    // if it is, set the flag
                    passwordMatchesUser = true;
                    // and exit the loop
                    break;
                }
            }
        }

        // afterwards, check if both flags are true
        if (foundUser && passwordMatchesUser) {
            JOptionPane.showMessageDialog(null, "Logged In");
        } else {
            JOptionPane.showMessageDialog(null, "Invalid Input");
        }

    } catch (FileNotFoundException e) {
        System.err.println("Workbook could not be found");
        e.printStackTrace();
    } catch (IOException e) {
        System.err.println("Workbook could not be read");
        e.printStackTrace();
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=23579&siteId=1