2017-10-11 11 views
0

我正在寫的JavaFX應用程序,模擬的Android設備店。開放不同的GUI基於MySQL的條件

如果用戶將在正確的用戶名和密碼,我想這個特定的方法來打開一個基於MySQL的數據庫表列"IsAdmin"內容不同的窗口。

"IsAdmin"列的內容是布爾值。

這裏的方法的代碼:

@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(); 
       } 
      } 
     }); 
    } 
} 

我不知道我應該怎麼寫一個正確的條件考慮"IsAdmin"領域,我沒能在網上找到任何東西。

我標誌着法空間,我希望把這些條件與"****"

這些條件應該看怎麼樣?還是有更好的方法來達到這個結果?

我將非常感謝任何答案或提示如何提高我的代碼。

謝謝你的時間。

回答

-1

基本上,我寧願做一個表的條件讓說:「用戶id」,它包含:

  • 用戶名
  • 密碼
  • 狀態(其中可能包含管理,最終用戶,或其他)

所以在Java代碼將是:

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"); 
     } 
    }); 
    } 
} 

這將完美的工作,因爲通過將列標籤設置爲「IsAdmin」來獲得條件的方式太糟糕而且效率不高。

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

上面的代碼會查找管理員列中的數值,條件:

if(status == "admin"){ 

}

將採取thhe條件,如果字符串狀態包含字符串「管理員」,那麼它將打開管理頁面,否則它將轉到客戶端頁面。

說明:您最好分離一個方法來調用fxml文件,並在需要時調用它。只需提供參數String fxmlFile,該參數將定義將在舞臺上設置的FXML文件。

+0

'status ==「admin」'[will not work。](https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – VGR