将数据库中的数据显示到tableView中

时间:2017-02-11 14:19:20

标签: java javafx tableview javafx-8

我正在尝试将数据库中的一些数据显示到TableView(JavaFX)中。

更新:     这是我的ControllerClass:

public class CustomersController implements Initializable   {
    @FXML
    private static TableView<Customers> tableView;
    @FXML
    private static TableColumn customerIDCol;
    @FXML
    private TableColumn<Customers, String> firstName;
    @FXML
    private TableColumn<Customers, String> lastName;
    @FXML
    private TableColumn<Customers, String> address;
    @FXML
    private Button addButton;
    @FXML
    private Button addRemove;
    @FXML
    private Button addUpdate;
    @FXML
    private TextField searchBar;
    @FXML
    private Button goButton;
    @FXML
    private Button bookButton;
    @FXML
    private ObservableList<Customers> data;
    /**
     * Initializes the controller class.
     */
    private static Connection con;
    private static Statement stat;
    private PreparedStatement prep;
    Database db = Database.getInstance();
    ResultSet rs = null;    

    public void initialize(URL url, ResourceBundle rb) {
        data = FXCollections.observableArrayList();
        try{
           String sql = "SELECT * FROM FRUITS ";
           rs = db.query(sql);
           con = DriverManager.getConnection("jdbc:sqlite:src/customers/fruits.db");
           stat = con.createStatement();
           while(rs.next()){
               Customers cn = new Customers();
               cn.firstName.set(rs.getString("FirstName"));
               data.add(cn);

               System.out.println(rs.getString("FirstName"));

           }
           firstName.setCellValueFactory(new PropertyValueFactory("firstName"));

           tableView.setItems(data);
           tableView.getColumns().add(firstName); 
       }
       catch (Exception e){
           e.printStackTrace();;
           System.out.println("error");
       }    
    }    

    @FXML
    private void handleAddButton(ActionEvent event) throws IOException{

    }

}

在控制台中打印firstName但是它在tableView.setItems(data)上给了我一个错误;

private static TableView<Customers> tableView;
private TableColumn<Customers, String> firstName;

塞特斯/吸气剂

 public  SimpleStringProperty firstName = new SimpleStringProperty();

 public String getFirstName() {
     return firstName.get();
 }
 public void setFirstName(String firstNameStr) {
     firstName.set(firstNameStr);
 }

控制台中的错误是:

java.lang.NullPointerException
at customers.CustomersController.initialize(CustomersController.java:98)

第98行是tableView.setItems(data);

1 个答案:

答案 0 :(得分:2)

Customers.java

public class Customers{
    public  SimpleIntegerProperty customerID = new SimpleIntegerProperty();
    public  SimpleStringProperty firstName = new SimpleStringProperty();
    public  SimpleStringProperty lastName = new SimpleStringProperty();
    public  SimpleStringProperty address = new SimpleStringProperty();
    public  SimpleStringProperty customerType = new SimpleStringProperty();
    public  SimpleStringProperty phone = new SimpleStringProperty();
    public  SimpleStringProperty email = new SimpleStringProperty();

    public int getCustomerID() {
        return this.customerID.get();
     }
    public void setCustomerID(int id) {
          this.customerID.set(id);
     }

    public String getFirstName() {
        return firstName.get();
     }
    public void setFirstName(String firstNameStr) {
          firstName.set(firstNameStr);
     }
    public String getLastName() {
        return lastName.get();
     }
    public void setLastName(String lastNameStr) {
          lastName.set(lastNameStr);
     }
    public String getAddress() {
        return address.get();
     }
    public void setAddress(String address) {
          this.address.set(address);
     }
    public void setCustomerType(String type) {
          this.customerType.set(type);
     }

    public String getCustomerType() {
        return this.customerType.get();
     }
    public void setPhone(String phone) {
          this.phone.set(phone);
     }
    public String getPhone() {
        return this.phone.get();
     }
    public void setEmail(String email) {
         this.email.set(email);
     }
    public String getEmail() {
        return this.email.get();
     }

}

Conrtoller类

public class CustomersController implements Initializable   {
    @FXML
    private static TableView<Customers> tableView;
    @FXML
    private static TableColumn<Customers, int> customerIDCol;
    @FXML
    private TableColumn<Customers, String> firstName;
    @FXML
    private TableColumn<Customers, String> lastName;
    @FXML
    private TableColumn<Customers, String> CustomerType;
    /**
     * Initializes the controller class.
     */
    private ObservableList<Customers> data;
    private static Connection con;
    private static Statement stat;
    private PreparedStatement prep;
    Database db = Database.getInstance();
    ResultSet rs = null;


    public void initialize(URL url, ResourceBundle rb) {
        data = FXCollections.observableArrayList();
       try{

           while(rs.next()){
               data.add(new Customers(
                    rs.getInt("CustomerID"),
                    rs.getString("FirstName"),
                    rs.getString("LastName"),
                    rs.getString("CustomerType"),
                    rs.getString("Address"),
                    rs.getString("Phone"),
                    rs.getString("Email")
               ));
           }
           CustomerIDCol.setCellValueFactory(new PropertyValueFactory("customerID"));
           firstName.setCellValueFactory(new PropertyValueFactory("firstName"));
           lastName.setCellValueFactory(new PropertyValueFactory("lastName"));
           CustomerType.setCellValueFactory(new PropertyValueFactory("customerTpe"));
           address.setCellValueFactory(new PropertyValueFactory("address"));
           phone.setCellValueFactory(new PropertyValueFactory("phone"));
           email.setCellValueFactory(new PropertyValueFactory("email"));

           tableView.setItems(data);             

       }catch (Exception e){
           e.printStackTrace();;
           System.out.println("error");
       }

    }    

    @FXML
    private void handleAddButton(ActionEvent event) throws IOException{


        }

    }

}

FXML代码

 <children>
            <TableView id="tableView" fx:id="tableView" prefHeight="272.0" prefWidth="887.0">
              <columns>
                <TableColumn id="CustomerIDCol" fx:id="customerIDCol" prefWidth="130.0" text="CustomerID" />
                <TableColumn id="firstName" fx:id="firstName" prefWidth="130.0" text="FirstName" />
                  <TableColumn id="lastName" fx:id="lastName" prefWidth="130.0" text="LastName" />
                  <TableColumn id="CustomerType" fx:id="CustomerType" prefWidth="130.0" text="CustomerType" />
                  <TableColumn id="address" fx:id="address" prefWidth="130.0" text="Address" />
                  <TableColumn fx:id="phone" prefWidth="130.0" text="Phone" />
                  <TableColumn fx:id="email" prefWidth="110.0" text="Email" />
              </columns>
            </TableView>
         </children>