获取两个数据库表值到一个JavaFX TreeTable

时间:2017-07-30 07:44:34

标签: javafx treetableview jxtreetable

我有两个类Customer和Product.i将此类值存储在数据库中,作为两个表,一个用于客户其他产品。我使用内部联接来检索这些数据。我想在JavaFX TreeTable中显示这些数据。这里有很多教程,但是它们只适用于一个类。这个代码到目前为止但是它没有工作。我不是javaFX专业版。

Connection con;
PreparedStatement ps;
ResultSet rs;

@FXML
private JFXButton close;

@FXML
private JFXTreeTableView<Product> detailsTable;


@FXML
private JFXComboBox<String> searchStatus;

@FXML
private JFXComboBox<String> searchLocation;

@FXML
private TreeTableColumn<Customer, String> cname;

@FXML
private TreeTableColumn<Customer, String> cnic;

@FXML
private TreeTableColumn<Customer, String> cphone;

@FXML
private TreeTableColumn<Product, String> pbrand;

@FXML
private TreeTableColumn<Product, String> ptype;

@FXML
private TreeTableColumn<Product, Integer> prcc;

@FXML
private TreeTableColumn<Product, String> plocation;

@FXML
private TreeTableColumn<Product, String> pshoptype;

@FXML
private TreeTableColumn<String, Date> pdate;


@FXML
void OnClickSearch(KeyEvent event) {

}

@FXML
void searchByLocation(ActionEvent event) {

}

@FXML
void searchByStatus(ActionEvent event) {

}

private ObservableList<Customer> cData;

private ObservableList<Product> pData;




@Override
public void initialize(URL url, ResourceBundle rb) {

@Override
public void initialize(URL url, ResourceBundle rb) {

    con= DBConnect.connect();
    cData=FXCollections.observableArrayList();
    pData= FXCollections.observableArrayList();
    try {
        rs=con.createStatement().executeQuery("SELECT c.customerName,c.customerNic,c.customerPhone,p.productBrand,p.productType,p.productRcc,p.productLocation,p.productShopType,p.productDate From customer as c INNER JOIN product as p ON c.customerId=p.customerId ");
    } catch (SQLException e) {
        System.out.println("GET DATA TO TABLE ERROR");
        e.printStackTrace();
    }

    try {
        while(rs.next()){

            //pData.add(rs.getString(1),rs.getString(2),rs.getString(3),rs.getString(4),rs.getString(5),rs.getInt(6),rs.getString(7),rs.getString(8),rs.getDate(9));
            cData.add(new Customer(rs.getString(1),rs.getString(2),rs.getString(3)));
            pData.add(new Product (rs.getString(4),rs.getString(5),rs.getInt(6),rs.getString(7),rs.getString(8),rs.getDate(9)));

        }
    } catch (SQLException e) {
        System.out.println("RS.Next() Error");
        e.printStackTrace();
    }

    cname.setCellValueFactory(new PropertyValueFactory<Customer,String>("customerNam"));
    cnic.setCellValueFactory(new PropertyValueFactory<Customer,String>("nic"));
    cphone.setCellValueFactory(new PropertyValueFactory<Customer,String>("phone"));
    pbrand.setCellValueFactory(new PropertyValueFactory<Product,String>("brand"));
    ptype.setCellValueFactory(new PropertyValueFactory<Product,String>("type"));
    prcc.setCellValueFactory(new PropertyValueFactory<Product,Integer>("rccNo"));
    plocation.setCellValueFactory(new PropertyValueFactory<Product,String>("location"));
    pshoptype.setCellValueFactory(new PropertyValueFactory<Product,String>("shopType"));
    pdate.setCellValueFactory(new PropertyValueFactory<Customer,Date>("date"));


    System.out.println("For Testing");
}

0 个答案:

没有答案