2 回答

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()方法,它將傳遞用戶。
在上面的例子中,我只是傳遞電子郵件,但你明白了。

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){
}
添加回答
舉報