0

I'm writing an javafx app that simulates android device shop.

I'd like this specific method to open a different window based on content of "IsAdmin" column in mysql database table if user puts in correct password and username.

Contents of "IsAdmin" column are boolean.

Here's the code of the method:

@FXML
private Button LoginButton;

@FXML
private TextField UserField;

@FXML
private PasswordField PassField;

@FXML
private Label ManagerLoginLabel;

@FXML
private Label StatusLabel;

@FXML
private Button RegisterButton;

@FXML
public void Login() {
    LoginButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
        public void handle(ActionEvent event) {
            PreparedStatement ps;
            try {
                Connection connection = DbUtil.getInstance().getConnection();
                ps = connection.prepareStatement("SELECT `Username`, `Password`, `IsAdmin` FROM `androidshop`.`userdatabase` WHERE `username` = ? AND `password` = ?");
                ps.setString(1, UserField.getText());
                ps.setString(2, String.valueOf(PassField.getText()));
                ResultSet result = ps.executeQuery();
                if (result.next() ****) {
                    StatusLabel.setText("Welcome, shop manager!");
                    try {
                        Parent parent;
                        parent = FXMLLoader.load(getClass().getClassLoader().getResource("view/ManagerPane.fxml"));
                        Stage stage = new Stage();
                        stage.setTitle("ManagerPane");
                        stage.setScene(new Scene(parent, 450, 450));
                        stage.show();
                        ((Node)(event.getSource())).getScene().getWindow().hide();
                    }
                        catch (IOException e) {
                            e.printStackTrace();
                             } else if (result.next() ****){
                            Parent parent;
                            StatusLabel.setText("Welcome, honored customer!");
                            parent = FXMLLoader.load(getClass().getClassLoader().getResource("view/ClientPane.fxml"));
                            Stage stage = new Stage();
                            stage.setTitle("ClientPane");
                            stage.setScene(new Scene(parent, 450, 450));
                            stage.show();
                            ((Node)(event.getSource())).getScene().getWindow().hide();

                        }
                     else {
                        StatusLabel.setText("Wrong Password/Username, please try again");
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

I didn't know how should I write a correct condition considering "IsAdmin" field and I wasn't able to find anything online.

I marked space in method where I wanted to put those conditions with "****".

How should those conditions look like? Or is there a better method of acheiving this result?

I will be very grateful for any answers or tips how to improve my code.

Thank you for your time.

Aldeguer
  • 821
  • 9
  • 32

1 Answers1

-1

basically, i prefer to make a condition with a table let say "userid" which contain :

  • username
  • password
  • status (which may contain admin, endUser, or whatever)

so in java code will be :

public void Login() {
    LoginButton.setOnAction(new EventHandler<ActionEvent>() {
        @Override
    public void handle(ActionEvent event) {
        PreparedStatement ps;
        try {
            Connection connection = DbUtil.getInstance().getConnection();
            String sql = "SELECT username, password, status FROM userid WHERE username = ? AND password = ?";
            ps = connection.prepareStatement(sql);
            ps.setString(1, UserField.getText());
            ps.setString(2, String.valueOf(PassField.getText()));
            ResultSet result = ps.executeQuery();
            if (result.next()) {
                String status = result.getString("status");
                if(status == "admin"){
                    StatusLabel.setText("Welcome, shop manager!");
                    try {
                        Parent parent;
                        parent = FXMLLoader.load(getClass().getClassLoader().getResource("view/ManagerPane.fxml"));
                        Stage stage = new Stage();
                        stage.setTitle("ManagerPane");
                        stage.setScene(new Scene(parent, 450, 450));
                        stage.show();
                        ((Node)(event.getSource())).getScene().getWindow().hide();
                    }
                    catch (IOException e) {
                        e.printStackTrace();
                    }
                }else{
                    try {
                        Parent parent;
                        StatusLabel.setText("Welcome, honored customer!");
                        parent = FXMLLoader.load(getClass().getClassLoader().getResource("view/ClientPane.fxml"));
                        Stage stage = new Stage();
                        stage.setTitle("ClientPane");
                        stage.setScene(new Scene(parent, 450, 450));
                        stage.show();
                        ((Node)(event.getSource())).getScene().getWindow().hide();
                    } catch (Exception e) {
                        System.out.println("Failed to Show Stage "+ e);
                    }
                }                     
            }                
        }catch (SQLException e) {
            StatusLabel.setText("Wrong Password/Username, please try again");
        }
    });
    }
}

that will work perfectly, because the way you get condition by giving the column label as "IsAdmin" is way too bad and not effective.

String status = result.getString("status"); 

the code above will look for the value inside admin column, and the condition :

if(status == "admin"){

}

will take thhe condition if the string status contain a string "admin", then it will open admin page, else it will go to client page.

note : it is better for you to make a separated method to call fxml file and will be called whenever you need it. just give parameter String fxmlFile that will define the FXML file that will be set on stage.

  • 1
    `status == "admin"` [will not work.](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – VGR Oct 11 '17 at 14:00