0

it always shows the infobox that i am always failing to get the string value of password and password 1 to confirm if those two are the same and then if it is the same it will insert those values into my local database

@FXML
    private void handleButtonActionRegister(ActionEvent event) {
        String email = textUserRegister.getText();
        String password = textRegisterPassword.getText();
        String password1 = textRegisterPassword1.getText();
        if (password != password1){
            infoBox("Password doesn't match!", "Failed", null);}
        else{
        String sql = "INSERT INTO employee VALUES (?,?)";
        try{
            preparedStatement = dbconnect.prepareStatement(sql);
            preparedStatement.setString(1, email);
            preparedStatement.setString(2, password);
            resultSet = preparedStatement.executeQuery();
            
            Node source = (Node) event.getSource(); 
            dialogStage = (Stage) source.getScene().getWindow();
            dialogStage.close();
            scene = new Scene(FXMLLoader.load(getClass().getResource("WelcomePage.fxml")));
            dialogStage.setScene(scene);
            dialogStage.show();  
       }catch(Exception e){e.printStackTrace();}}
    }
MrTux
  • 32,350
  • 30
  • 109
  • 146
AKWARD
  • 13
  • 3
  • `if (password != password1)` don't compare strings with ==. https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java – markspace Feb 02 '21 at 08:02

1 Answers1

1

You should compare String values with .equals() method not with "==". Correct code below:

@FXML
private void handleButtonActionRegister(ActionEvent event) {
    String email = textUserRegister.getText();
    String password = textRegisterPassword.getText();
    String password1 = textRegisterPassword1.getText();
    if (!password.equals(password1)){
        infoBox("Password doesn't match!", "Failed", null);}
    else{
    String sql = "INSERT INTO employee VALUES (?,?)";
    try{
        preparedStatement = dbconnect.prepareStatement(sql);
        preparedStatement.setString(1, email);
        preparedStatement.setString(2, password);
        resultSet = preparedStatement.executeQuery();
        
        Node source = (Node) event.getSource(); 
        dialogStage = (Stage) source.getScene().getWindow();
        dialogStage.close();
        scene = new Scene(FXMLLoader.load(getClass().getResource("WelcomePage.fxml")));
        dialogStage.setScene(scene);
        dialogStage.show();  
   }catch(Exception e){e.printStackTrace();}}
}
arch2be
  • 306
  • 2
  • 8