【问题标题】:Opening different GUI in javafx app based on mysql condition根据mysql条件在javafx应用程序中打开不同的GUI
【发布时间】:2017-10-11 12:32:49
【问题描述】:

我正在编写一个模拟 android 设备商店的 javafx 应用程序。

如果用户输入正确的密码和用户名,我希望这种特定方法可以根据 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" 字段,我不知道应该如何编写正确的条件,并且我无法在网上找到任何内容。

我在方法中用"****" 标记了我想放置这些条件的空格。

这些条件应该是什么样子?或者有没有更好的方法来实现这个结果?

我将非常感谢任何关于如何改进我的代码的答案或提示。

感谢您的宝贵时间。

【问题讨论】:

    标签: java mysql javafx


    【解决方案1】:

    基本上,我更喜欢用一个表格来创建一个条件,比如说“userid”,其中包含:

    • 用户名
    • 密码
    • 状态(可能包含 admin、endUser 或其他)

    所以在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"); 
    

    上面的代码将在 admin 列中查找值和条件:

    if(status == "admin"){
    

    }

    如果字符串status包含字符串“admin”,则取该条件,则打开管理页面,否则进入客户端页面。

    注意:最好单独创建一个调用fxml文件的方法,在需要的时候调用。只需提供参数 String fxmlFile,它将定义将在舞台上设置的 FXML 文件。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 2013-09-24
    • 1970-01-01
    相关资源
    最近更新 更多