缺少JavaFX中查询的SQL数据库中的字段

时间:2016-03-02 06:28:30

标签: sqlite javafx

我很难理解为什么我的3个SQL字段没有显示出来。我已经尝试调试我如何编写代码的不同方面,没有运气。我的代码如下。 UsersController用于fxml文件,该文件显示各种用户的tableView及其相关信息。它是从一个选项卡中的MainController调用的。 UsersController代码是:

@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");            
     }
}

这个的fxml代码是:

 <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>

从MainController调用新选项卡的setOnAction如下:

@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();
    }
}

由于某种原因,ID,用户名,密码和电子邮件都显示在表中,但first_name,last_name和user_level不显示。我已经多次阅读了这段代码并且已经使用了几个小时,并且无法找到为什么这三个字段没有出现。我已经更改了变量名称,注释掉了代码段以查看发生了什么,并且已经搞乱了新的代码片段无济于事。请帮忙,谢谢!

0 个答案:

没有答案