mardi 1 mars 2016

Missing fields from a queried SQL database in JavaFX

I am having a difficult time understanding why 3 of my SQL fields are not showing up. I have tried debugging different aspects of how I have the code written with no luck. My code is as follows. The UsersController is for the fxml file that shows a tableView of various users and associated information about them. It is called from the MainController in a tab. The UsersController code is:

@Override
public void initialize(URL location, ResourceBundle resources) {
    // TODO Auto-generated method stub

     assert userTable != null;
     iCol.setCellValueFactory(
           new PropertyValueFactory<User, Integer>("id"));
     fnCol.setCellValueFactory(
            new PropertyValueFactory<User, String>("first_name"));
     lnCol.setCellValueFactory(
            new PropertyValueFactory<User, String>("last_name"));
     unCol.setCellValueFactory(
            new PropertyValueFactory<User, String>("username"));  
     pCol.setCellValueFactory(                
        new PropertyValueFactory<User, String>("password"));
     emCol.setCellValueFactory(
            new PropertyValueFactory<User, String>("email"));
     aCol.setCellValueFactory(
                new PropertyValueFactory<User, String>("user_level"));
    // Establish a connection to the database.
     DBConnection dBC = new DBConnection();
     con = dBC.getDBConnection();

     listNum = 1;        
     userData = FXCollections.observableArrayList();
     try{   
        String userQuery = "Select * from users";             //Order By id
        ResultSet result = con.createStatement().executeQuery(userQuery);  
        while(result.next()){

            User u = new User();
            u.id.set(listNum++);                       
            u.first_name.set(result.getString("firstname"));
            u.last_name.set(result.getString("lastname"));
            u.username.set(result.getString("username"));
            u.password.set(result.getString("password"));
            u.email.set(result.getString("email"));
            u.user_level.set(result.getString("userlevel"));
            userData.add(u); 
            System.out.println(u.first_name);
        }

        userTable.setItems(userData);
     }
     catch(Exception e){
          e.printStackTrace();
          System.out.println("Error on Building Data");            
     }
}

the fxml code for this is:

 <columns>
                                <TableColumn fx:id="iCol" prefWidth="41.0" text="List" />
                              <TableColumn fx:id="fnCol" prefWidth="149.0" text="First Name" />
                              <TableColumn fx:id="lnCol" prefWidth="165.0" text="Last Name" />
                                <TableColumn fx:id="unCol" prefWidth="192.0" text="Username" />
                                <TableColumn fx:id="pCol" prefWidth="191.0" text="Password" />
                                <TableColumn fx:id="emCol" prefWidth="310.0" text="Email" />
                                <TableColumn fx:id="aCol" prefWidth="151.0" text="Administrative Rights" />
                            </columns>

The setOnAction that calls the new tab from the MainController is as follows:

@FXML public void userWindow(ActionEvent event){

    try {
        Tab tab = new Tab();
        tabs.getTabs().add(tab);
        tab.setText("Users");
        tab.setContent((Node) FXMLLoader.load(this.getClass().getResource("Users.fxml")));
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}

For some reason the id, username, password, and email all show up in the table, but the first_name, last_name, and user_level do not. I have read through the code many times and have fiddled with it for hours now and can not find why those 3 fields do not show up. I have changed variable names, commented out sections of code to see what happens, and have messed with new snippets of code to no avail. Please help, thanks!

Aucun commentaire:

Enregistrer un commentaire