0

I fairly new to Java and JavaFX am trying to write a program that will pull user data from a MYSQL database. My connection works, but I am unable to pull the information into the TableView. Now I have reworked this so many times that now I have no idea where I am especially since I am not getting errors.

Can someone please show me how to make this work properly.

Here is the Users Class

public class Users {
    
    public Integer userID;
    public String userName;
    public String password;
    

    
    public Users(Integer userID, String userName, String password) {
        this.userID = userID;
        this.userName = userName;
        this.password = password;
    }
    

    private static final ObservableList<Users> allUsersList = FXCollections.observableArrayList();
         
        /** This method adds Users to the all Users observable list.
        * @param Users */
        public static void addUsers(Users users) {
         allUsersList.add(users);
        }
     
        /** This method gets Users from the all Users observable list.
        * @return all Users*/
        public static ObservableList<Users> getAllUsers() {
            return allUsersList;
        }

    
        /** This method modifies Users on the Users observable list
         * @param newUsers.*/
        public void modifyUsers(Users newUsers) { 
        }
        
        /** This method deletes Users from the Users observable list
        * @param selectedUser.*/
        public void deleteUsers(Users selectedUser) {
        }
        
  
        public int getUserID() {
        return userID;
        }

        public void setUserID(int userID) {
        this.userID = userID;
        }

        public String getUserName() {
        return userName;
        }

        public void setUserName(String userName) {
        this.userName = userName;
        }

        public String getPassword() {
        return password;
        }

        public void setPassword(String password) {
        this.password = password;
        }

        public void setUser(int userID, String userName, String password) {
        }
    
    
    }

Here is the Controller Class

public class UsersScreenController implements Initializable {

    @FXML
    public TableView<Users> userTable;
    @FXML
    private TextField userID;
    @FXML
    private TextField userName;
    @FXML
    private TextField password;
    @FXML
    private TableColumn<Users, Integer> usernameIDCol;
    @FXML
    private TableColumn<Users, String> userNameCol;
    @FXML
    private TableColumn<Users, String> passwordCol;
    @FXML
    private Button Dashboard;
    @FXML
    private Button editUser;
    @FXML
    private Button newUserBtn;
    @FXML
    private Button saveUserBtn;
    @FXML
    private Button resetUserBtn;
    
    
    
    public UsersScreenController() {
       
    
    }
    
    /**
     * Initializes the controller class.
     */
    
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        userTable.setItems(Users.getAllUsers());
        
        usernameIDCol.setCellValueFactory(new PropertyValueFactory<>("User_ID"));
        userNameCol.setCellValueFactory(new PropertyValueFactory<>("User_Name"));
        passwordCol.setCellValueFactory(new PropertyValueFactory<>("Password"));
    
    }

    // Generates unique id for each new user as added
    
    AtomicInteger count = new AtomicInteger(4);

    /** This method adds a new user to the Users table
     * @param actionEvent
     */
    
    @FXML
    public void newUser(ActionEvent actionEvent) throws IOException {
        int userIDS = count.incrementAndGet();
        String userNameS = userNameCol.getText();
        String passwordS = passwordCol.getText();
        
    //    Users.addUser(new User(int userID, String userName, String password));
        
        Parent root = FXMLLoader.load(getClass().getResource("/View/UsersScreen.fxml"));
            Stage stage = (Stage)((Node)actionEvent.getSource()).getScene().getWindow();
            Scene scene = new Scene(root);
            stage.setScene(scene);
            stage.show();  
        
    }
    
    /**Sets modified users to show in user table
    * @param users 
    **/
    @FXML
    public void setUser(ActionEvent event){
        String userIDS=userID.getText();
        String userNameS=userName.getText();
        String passwordS=password.getText();
        
        userTable.setItems(Users.getAllUsers());
        
        usernameIDCol.setCellValueFactory(new PropertyValueFactory<>("User_ID"));
        userNameCol.setCellValueFactory(new PropertyValueFactory<>("User_Name"));
        passwordCol.setCellValueFactory(new PropertyValueFactory<>("Password"));
        
        
    }

    public void editUser(ActionEvent actionEvent) throws IOException {
            
        TableViewSelectionModel<Users> selectionModel = userTable.getSelectionModel();
        
        ObservableList<Users> selectedItems = selectionModel.getSelectedItems();
        
        Users users = null;

        users = userTable.getSelectionModel().getSelectedItem();
        
        if (users == null) {
            Alert errorAlert = new Alert(AlertType.ERROR);
            errorAlert.setTitle("Missing Selection Error");
            errorAlert.setContentText("Missed item selection. Can you select the user you want to modify?");

            errorAlert.show();
            return;
        }
        
        int userIDS = users.getUserID();
        String userNameS = users.getUserName();
        String passwordS = users.getPassword();

        FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/UsersScreen.fxml"));
        
        Parent root = loader.load();
        Users user = loader.getController();
        Stage stage = (Stage)((Node)actionEvent.getSource()).getScene().getWindow();
        stage.setScene(new Scene(root));
        
        stage.show();
        
        }  
        
        


    @FXML
    private void deleteUser(ActionEvent actionEvent) {
       Alert alert = new Alert(AlertType.CONFIRMATION);
       alert.setTitle("Delete User");
       alert.setContentText("Are you sure you wish to delete user?");
       
       if(alert.showAndWait().get() == ButtonType.OK) {
       userTable.getItems().remove(userTable.getSelectionModel().getSelectedItem());
       }
        
    }
    
    
    public void saveUser(ActionEvent actionEvent) throws IOException {
            
        TableViewSelectionModel<Users> selectionModel = userTable.getSelectionModel();
        
        ObservableList<Users> selectedItems = selectionModel.getSelectedItems();
        
        Users users = null;

        users = userTable.getSelectionModel().getSelectedItem();
        
        if (users == null) {
            Alert errorAlert = new Alert(AlertType.ERROR);
            errorAlert.setTitle("Missing Selection Error");
            errorAlert.setContentText("Missed item selection. Can you select the user you want to modify?");

            errorAlert.show();
            return;
        }
        
        int userIDS = users.getUserID();
        String userNameS = users.getUserName();
        String passwordS = users.getPassword();

        FXMLLoader loader = new FXMLLoader(getClass().getResource("/View/UsersScreen.fxml"));
        
        Parent root = loader.load();
        Users user = loader.getController();

        
        Stage stage = (Stage)((Node)actionEvent.getSource()).getScene().getWindow();
        stage.setScene(new Scene(root));
        
        stage.show();
        
        }  
    
    

    /** This method brings user back to main screen when clicking cancel button. 
     * @param actionEvent
     */ 
    @FXML
    private void resetUser(ActionEvent event) throws IOException {

    }
    
    
    /** This method brings user back to Dashboard screen when clicking Dashboard button. 
     * @param actionEvent
     */ 
    @FXML
    private void goToDashBoard(ActionEvent event) throws IOException {
         Stage primaryStage = new Stage();
                   Parent root = FXMLLoader.load(getClass().getResource("/View/Dashboard.fxml"));
                   Scene scene = new Scene(root);
                   primaryStage.setScene(scene);
                   primaryStage.setTitle("SCHEDULER");
                   primaryStage.show();
    }

I think this might be the one causing the issue, where I call the prepared statement.

public class DBUsers {
    
    private static ObservableList<Users> getAllUsers(){
            
        ObservableList<Users> userData = FXCollections.observableArrayList();
        
        try{
            String sql = "SELECT * from users";
            
            PreparedStatement ps = DBConnection.getConn().prepareStatement(sql);
            
            ResultSet rs = ps.executeQuery(sql);
            
            while (rs.next()) {
                int userID = rs.getInt("User_ID");
                String userName = rs.getString("User_Name");
                String password = rs.getString("Password");
                UsersScreenController.userTable.getColumns().addAll(users);
            }
        } 
        
        catch (SQLException e) {
            e.printStackTrace();
        }

        return userData;
    }

   
    
}

Finally the Main

public class Main extends Application {
    
    /**
     *
     * @param primaryStage
     * @throws Exception
     */
     @Override
    public void start(Stage primaryStage) throws Exception  {
              
            Parent root = FXMLLoader.load(getClass().getResource("/View/LoginForm.fxml"));
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.setTitle("SCHEDULER");
            primaryStage.show();
        } 
   
    
    
    /**
     * runs args then calls closeConn() to close database connections
     * @param args 
     * @throws java.lang.Exception 
     */
    public static void main(String[] args) throws Exception {
    Locale.setDefault(new Locale("fr", "FR"));
      System.out.println(Locale.getDefault()); 
      
     DBConnection.openConnection();

     launch(args);
              
      DBConnection.closeConnection();        
        
      
        

    }


   
    }    
Dan Bron
  • 2,313
  • 1
  • 22
  • 35
  • 2
    you have to follow the naming pattern (of your setters/getters in the data class) __and__ use those names in the PropertyValueFactory (which is strongly recommended to __not__ use!) Please work through a tutorial on how to use TableView to understand what you need (or at the very least study the api doc of TableView/Column and PropertyValueFactory, if you insist on using it) – kleopatra Jan 14 '22 at 17:02
  • 1
    @kleopatra's point is also examined in detail [here](https://stackoverflow.com/q/68969222/230513). – trashgod Jan 14 '22 at 17:43

0 Answers0