Java FXML登录表单问题:数据库参数无法传递至下一表单
Hey there! Let's figure out why your librarian's data isn't getting passed to the next screen after login. Looking at your code snippet, the main issue is that you're only verifying the login credentials (returning a boolean) but not capturing the actual Librarian object from the database, and you're not passing any user data to the next screen's controller. Here's how to fix this step by step:
1. Adjust Your Database Fetch Logic
Right now, your isLogin() method only tells you if credentials are valid—but you need the full Librarian record to pass along. Let's modify (or replace) that method to return the actual user object when credentials are correct, or null if they're not:
// Inside your Librarian class public Librarian getLibrarianByCredentials(String username, String password) { // Replace with your actual database connection/query logic String query = "SELECT * FROM librarians WHERE username = ? AND password = ?"; try (Connection conn = YourDBConnectionClass.getConnection(); PreparedStatement pstmt = conn.prepareStatement(query)) { pstmt.setString(1, username); pstmt.setString(2, password); ResultSet rs = pstmt.executeQuery(); if (rs.next()) { // Populate a Librarian object with data from the result set Librarian loggedInLibrarian = new Librarian(); loggedInLibrarian.setId(rs.getInt("id")); loggedInLibrarian.setUsername(rs.getString("username")); loggedInLibrarian.setEmail(rs.getString("email")); // Add other fields (like name, role, etc.) from your DB table return loggedInLibrarian; } } catch (SQLException e) { e.printStackTrace(); } return null; // Invalid credentials }
2. Pass the Librarian Object to the Next Screen's Controller
Update your LibrarianLogin() method to fetch the user object, then pass it directly to the admin screen's controller when loading the FXML:
@FXML public void LibrarianLogin(ActionEvent event) { try { String inputUsername = this.username.getText().trim(); String inputPassword = this.password.getText().trim(); // Get the full Librarian object from the database Librarian loggedInLibrarian = this.l1.getLibrarianByCredentials(inputUsername, inputPassword); if (loggedInLibrarian != null) { // Close the login window Stage loginStage = (Stage) this.loginButton.getScene().getWindow(); loginStage.close(); // Open the admin screen and pass the user data openAdminDashboard(loggedInLibrarian); } else { // Show error for invalid credentials this.loginStatus.setText("Invalid username or password!"); } } catch (Exception e) { e.printStackTrace(); this.loginStatus.setText("Login failed: " + e.getMessage()); } } private void openAdminDashboard(Librarian librarian) throws IOException { // Load the admin FXML file (update the path to match your project structure) FXMLLoader loader = new FXMLLoader(getClass().getResource("/com/yourproject/views/AdminDashboard.fxml")); Parent root = loader.load(); // Get the admin controller instance and pass the Librarian object AdminController adminController = loader.getController(); adminController.setLoggedInLibrarian(librarian); // Launch the admin stage Stage adminStage = new Stage(); adminStage.setTitle("Librarian Admin Dashboard"); adminStage.setScene(new Scene(root)); adminStage.show(); }
3. Update the Admin Controller to Receive the Data
In your AdminController, add a setter method to accept the Librarian object, then use it to populate the UI or perform actions:
public class AdminController implements Initializable { private Librarian loggedInLibrarian; // Example UI element (add this to your AdminDashboard.fxml if needed) @FXML private Label welcomeLabel; // Setter to receive the logged-in librarian data public void setLoggedInLibrarian(Librarian librarian) { this.loggedInLibrarian = librarian; // Use the data to update the UI if (welcomeLabel != null) { welcomeLabel.setText("Welcome back, " + librarian.getUsername() + "!"); } } // Rest of your admin controller logic... @Override public void initialize(URL url, ResourceBundle resourceBundle) { // Initialization code here } }
Alternative: Global Session Management (For Multi-Screen Apps)
If you need access to the logged-in user across multiple screens, create a singleton SessionManager class to store the user data globally:
public class SessionManager { private static SessionManager instance; private Librarian loggedInLibrarian; // Private constructor to enforce singleton pattern private SessionManager() {} public static SessionManager getInstance() { if (instance == null) { instance = new SessionManager(); } return instance; } public void setLoggedInLibrarian(Librarian librarian) { this.loggedInLibrarian = librarian; } public Librarian getLoggedInLibrarian() { return loggedInLibrarian; } }
Then in your login method, set the session user:
if (loggedInLibrarian != null) { SessionManager.getInstance().setLoggedInLibrarian(loggedInLibrarian); // Close login stage and open admin screen as before }
And in any controller that needs the user data:
Librarian currentUser = SessionManager.getInstance().getLoggedInLibrarian(); // Use currentUser for actions like fetching their specific data, etc.
内容的提问来源于stack exchange,提问作者Mohamed Essam

