为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

潇湘沐 2023-08-09 15:30:57
我正在尝试使用 JavaFX 创建一个应用程序。我想在成功登录后获取当前登录的用户 ID 和用户名。我想将其显示在主页中。我怎样才能做到这一点?请帮忙MediaController.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());        }    }
查看完整描述

2 回答

?
慕码人2483693

TA贡献1860条经验 获得超9个赞

让我说清楚,您有用户登录,然后将场景更改为主窗口,但您想记住登录的用户并在主页上显示该用户名吗?


听起来您必须在场景之间传递数据。


为此,您需要在面向对象编程中解决这个问题。有一个代表您的用户的对象类以及所有 getter 和 setter。


public class User {


    private String email;

    


    public User(String email) {

        this.email = email;

    

    }


    public String getEmail() {

        return email;

    }


}

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


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”类或其他东西。


喜欢


public void initData(User user) {

        selectedUser = user;

        labelUser.setText(selectedUser.getEmail());

    }

然后,在验证后,从您的登录类将数据发送到主窗口,然后通过实例化您的 User 来更改场景,然后将对象从第二个场景传递给 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()方法,它将传递用户。


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


查看完整回答
反对 回复 2023-08-09
?
缥缈止盈

TA贡献2041条经验 获得超4个赞

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


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){

        }


查看完整回答
反对 回复 2023-08-09
  • 2 回答
  • 0 关注
  • 107 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信