繁体   English   中英

使用 JavaFX 获取当前记录的用户 ID 和用户名

[英]Get Current logged User Id and User Name using JavaFX

我正在尝试使用 JavaFX 创建一个应用程序。 成功登录后,我想获取当前登录的用户名和用户名。 我想在主页显示它。 我怎样才能做到这一点? 请帮忙

媒体控制器.java

    @FXML
    private Label tf_getname;

    @FXML
    void happyButton(ActionEvent event) {

        DbConnect dbconnect=new DbConnect();
        Connection conn=dbconnect.getConnection();

        String username = tf_getname.getText();

//        String source1 = event.getSource().toString(); //yields complete string
        //String source2 = event.getPickResult().getIntersectedNode().getId(); //returns JUST the id of the object that was clicked
//        System.out.println("Full String: " + source1);
//        System.out.println("Just the id: " + source2);
//        System.out.println(" " + source2);

        try {

            String sql = "SELECT name FROM users WHERE name='"+username+"'";

            Statement stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery(sql);

            while(rs.next()){
                tf_getname.setText(rs.getString("name"));

            }


        } catch (Exception e) {
            System.err.println(e.getMessage());
        }

    }

我认为你的说法有问题。 尝试以下方法来设置和执行语句。

Statement stmt = con.createStatement();
        ResultSet rs = stmt.executeQuery("Select * from test");
        while(rs.next()){
            System.out.println(rs.getString("name"));
            con.close();
        }
        }catch(Exception e){
        }

让我直截了当地说,您有用户登录,然后将场景更改为主 window,但是您想记住登录的用户并在主页上显示该用户名?

听起来您将不得不在场景之间传递数据。

为此,您需要在 OOP 中解决此问题。 有一个 object class 用所有的 getter 和 setter 代表您的用户。

public class User {

    private String email;
    

    public User(String email) {
        this.email = email;
    
    }

    public String getEmail() {
        return email;
    }

}

例如,当您在登录时连接到数据库时,验证用户,然后实例化“用户”class 的 object,然后将其传递到您正在加载的主窗口场景。

public class LoginController implements Initializable {
    public User user;


    // All your FXML code



  @FXML
void handleLogin(ActionEvent actionEvent) throws IOException {
    // Do your validation and then call the changeToMainWindow()
    changeToMainWindow();
}

}

在主窗口控制器中有一个“initData”class 或其他东西。

喜欢

public void initData(User user) {
        selectedUser = user;
        labelUser.setText(selectedUser.getEmail());
    }

然后从您的登录 class 中,在验证后,通过实例化您的用户,然后将 object 从您的第二个场景传递给 initData 方法,将数据发送到主窗口,然后再更改您的场景。

//User validation, then:
// Get the FXMLLoader then
//Instantiate the mainwindow controller: 

 public void changeToMainWindow() throws IOException {
        FXMLLoader loader = new FXMLLoader();
        loader.setLocation(getClass().getResource("mainwindow.fxml"));
        Parent root = loader.load();
        Scene mainScene = new Scene(root);

        // access the controller
        MainWindowController mainWindowController = loader.getController();
        mainWindowController.initData(user);

        Stage primaryStage = (Stage) loginButton.getScene().getWindow();
        primaryStage.setScene(mainScene);
        primaryStage.show();
    }

然后在登录时,使用 changeToMainWindow() 方法,它将传递用户。

在上面的例子中,我只是传递了 email,但你明白了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM