how can switch between scenes if the condition is true JavaFX

As @James_D already said you have to pass a non-null event to get the stage variable, and you should also change the scene in the same stage instead of creating a new stage again, ’cause this will not take care of resources and will slow down the application.

Also, looking at your login code, I would suggest you to get the database row only by username, and then compare passwords (better if they’re password hashes or much better with salted-hashes) as variables. This because resultset can be tricky and you don’t want to base your authentication on it.

public void validateLogin(Connection conn) {

    String username = username1.getText(); // you should obviously prevent username 
                                           // duplication in the signin page code
    String password = password1.getText();

    String queryStr = "select * from user_account where username=?";

    try (PreparedStatement prepStmt = conn.prepareStatement(queryStr)) {

        prepStmt.setString(1, username);
        ResultSet rset = prepStmt.executeQuery();

        if (!rset.next())
            throw new LoginFailedException(); // the username doesn't match any database 
                                              // row

        byte[] passwordHash = rset.getBytes("passwordColumn"); // get the password hash from 
                                                               // the resultset

        byte[] inputHash = someHashingFunction(password); // get the input hash with the same 
                                                          // function used to store passwords, 
                                                          // so that you get the same result 
                                                          // inputting the same password

        checkHashes(passwordHash, inputHash))
        switchScene(); // some method or block to switch scenes

    } catch (LoginFailedException e) {
        // show some error scene here
    }
}

public void checkHashes(byte[] passwordHash, byte[] inputHash) throw LoginFailedException {

    for (int x = 0; x < inputHash.length; x++) {
        if (passwordHash[x] != inputHash[x]) {
            throw new LoginFailedException();
    }        
}

Leave a Comment