I made a function in my program which alllows employees to login in an application I made. The code for that looks like this:
LoginController:
//Login for employee
@FXML
private TextField textUsername;
@FXML
private PasswordField textPassword;
@FXML
private ChoiceBox<String> EmpSelect;
Stage dialogStage = new Stage();
Scene scene;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
public LoginController() {
connection = sqlDatabaseConnection.connectdb();
}
//Login for employee
@FXML
private void handleButtonAction(ActionEvent event) {
String username = textUsername.getText().toString();
String password = textPassword.getText().toString();
String function = EmpSelect.getValue().toString();
String sql = "SELECT * FROM Employee WHERE username = ? and password = ? and function = ?";
try {
preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1, username);
preparedStatement.setString(2, password);
preparedStatement.setString(3, function);
resultSet = preparedStatement.executeQuery();
if (!resultSet.next()) {
infoBox("Enter Correct Username and Password", "Failed", null);
} else {
if ("Employee".equals(function)) {
infoBox("Login Successfull", "Success", null);
FXMLDocumentController controller = new FXMLDocumentController();
controller.newAnchorpane("WorkerHomescreen", paneLogin);
} else if ("Manager".equals(function)) {
infoBox("Login Successfull", "Success", null);
FXMLDocumentController controller = new FXMLDocumentController();
controller.newAnchorpane("WorkerHomescreen", paneLogin);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void infoBox(String infoMessage, String titleBar, String headerMessage) {
Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle(titleBar);
alert.setHeaderText(headerMessage);
alert.setContentText(infoMessage);
alert.showAndWait();
}
With this piece of code employees are able to login. This works all fine and dandy. As you can see with this code it checks in the choiceBox if you want to login as a employee or a manager, and than it redirects you to the correct page.
The thing is that if you login as a manager you will be able to see all the buttons and labels. And when you login as a employee you will be able to see only a few buttons and labels.
I want this to happen when the page loads after the login. And seeing as I want to login to the page called: WorkerHomescreen.fxml I thought it would be handy to put some code in this class.
WorkerHomescreen class:
Connection connection = null;
public WorkerHomescreenController() {
connection = sqlDatabaseConnection.connectdb();
}
@Override
public void initialize(URL url, ResourceBundle rb) {
String Check = "SELECT * FROM Employee WHERE function = 'employee'";
but4.setVisible(false);
}
So basically with this piece of code I want to make it so that the query checks if you logged in as a employee. And if that is the case the button called "but4" will become invisible. I tried this and it doesn't work. It hides the button even if you are logged in as a manager.
So I was wondering what I am doing wrong here and if someone can help me with this.
If I missed something please let me know and I will post it in the comments below.