使用 JavaFX 获取当前登录的用户 ID 和用户名
Get Current logged User Id and User Name using JavaFX
我正在尝试使用 JavaFX 创建应用程序。成功登录后,我想获取当前登录的用户名和用户名。我想显示它在主页上。我怎样才能做到这一点?请帮助
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());
}
}
我觉得你的说法有问题。尝试以下方式来设置和执行语句。
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 中解决这个问题。有一个对象 class 用所有的 getter 和 setter 代表您的用户。
public class User {
private String email;
public User(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
}
当您在登录时连接到数据库时,验证用户然后实例化“用户”的对象 class 例如,然后将其传递给您正在加载的主要 window 场景。
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();
}
}
在主window控制器中有一个“initData”class或其他东西。
喜欢
public void initData(User user) {
selectedUser = user;
labelUser.setText(selectedUser.getEmail());
}
然后从您的登录 class,在验证后,在通过实例化您的用户更改场景之前将数据发送到 mainwindow,然后将对象从第二个场景传递到 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() 方法,它会传递给用户。
在上面的示例中,我只是简单地传递了电子邮件,但你明白了。
我正在尝试使用 JavaFX 创建应用程序。成功登录后,我想获取当前登录的用户名和用户名。我想显示它在主页上。我怎样才能做到这一点?请帮助
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());
}
}
我觉得你的说法有问题。尝试以下方式来设置和执行语句。
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 中解决这个问题。有一个对象 class 用所有的 getter 和 setter 代表您的用户。
public class User {
private String email;
public User(String email) {
this.email = email;
}
public String getEmail() {
return email;
}
}
当您在登录时连接到数据库时,验证用户然后实例化“用户”的对象 class 例如,然后将其传递给您正在加载的主要 window 场景。
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();
}
}
在主window控制器中有一个“initData”class或其他东西。
喜欢
public void initData(User user) {
selectedUser = user;
labelUser.setText(selectedUser.getEmail());
}
然后从您的登录 class,在验证后,在通过实例化您的用户更改场景之前将数据发送到 mainwindow,然后将对象从第二个场景传递到 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() 方法,它会传递给用户。
在上面的示例中,我只是简单地传递了电子邮件,但你明白了。