JavaFXのはApache POIエクセルログインプログラム

青:

私は、Javaに新しいですし、私はしばらくの間、今それを研究してきました

私は情報を保存するために、Excelを使用してログインプログラムを作りたいです。

問題は、私はボタンでログをコーディングする方法がわからないということです。私が検索し、内部の値を見つけるために、イテレータを使用してこのコードを思い付きましたXSSFSheet

缶誰かの助け私?答える人のための最大の感謝。

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) {
    }
}

これは、Excelファイルであります

ここでは、画像の説明を入力します。

これはGUIです

ここでは、画像の説明を入力します。

deHaar:

あなたは本当に必要としないiteratorユーザー名とパスワードの列を知っていれば。あなたは、単に行インデックスによって反復処理し、直接インデックスで列にアクセスすることができます1し、2
次の例を見ると、慎重にコードのコメントをお読みください。

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();
    }
}

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=23831&siteId=1