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.