Java FX TableView数据库

时间:2015-11-14 19:21:20

标签: database javafx tableview

我现在正在使用Java FX应用程序3周,我有一个问题,我通过互联网搜索有关它的事情,但我找不到解决方案。我必须在tableview中的行上填充数据我从数据库获取数据,但我不能把它放在表中我有这个代码,我用它来从数据库中获取数据。

  public ObservableList<ObservableList> GetCustomerData(){
  ObservableList<ObservableList> Data = FXCollections.observableArrayList();
    try{
        ConnectToDB();
        DBStatement = DBConnection.createStatement();
        DataRetrive = DBStatement.executeQuery("SELECT * FROM Customer");
        while (DataRetrive.next()){
         ObservableList<String> Row = FXCollections.observableArrayList();
           Row.add(String.valueOf(DataRetrive.getInt(1)));
           Row.add(DataRetrive.getString(2));
           Row.add(DataRetrive.getString(3));
           Row.add(DataRetrive.getString(4));
           Row.add(DataRetrive.getString(5));
           Row.add(DataRetrive.getString(6));
           Row.add(DataRetrive.getString(7));
           Row.add(DataRetrive.getString(8));
           Row.add(DataRetrive.getString(9));          
          Data.add(Row);
        }
        CloseDB();
    }catch(SQLException e){
        e.printStackTrace();
    }
    return Data;
}

如何从此ObservableList中的表格单元格中添加数据?

1 个答案:

答案 0 :(得分:1)

优良作法是为 CRUD 操作设置单独的类:

public abstract class CustomerDML {

    public static final String TABLE_NAME = "Customer";
    QueryClass qc = new QueryClass();
    public ObservableList<Map> getCustomerData() {
    ObservableList<Map> productList = FXCollections.observableArrayList();
    try {
        qc.setPs(qc.getConn().prepareStatement("SELECT * FROM " + TABLE_NAME));
        qc.setRs(qc.getPs().executeQuery());
        while (qc.getRs().next()) {
            Map<String, String> product = new HashMap<>();
            product.put("cus_id", String.valueOf(qc.getRs().getInt("cus_id")));
            product.put("name", qc.getRs().getString("name"));
            product.put("age", String.valueOf(qc.getRs().getInt("age")));
            product.put("telephone", qc.getRs().getString("telephone"));
            productList.add(product);

        }
        return customerList;
    } catch (SQLException ex) {
        return null;
    }
}

您可以像下面这样实施 QueryClass

public class QueryClass {

private Connection conn;
private PreparedStatement ps;
private ResultSet rs;
//private String query;

public QueryClass() {
    conn = DBConnectionClass.myConnection();
}
//getters and setters
...

让我们假设我们已经从你的fxml中向你的控制器注入了类似下面的内容:

public class Customer_fxmlController extends CustomerDML implements Initializable {

@FXML
private TableView<Map> cusTable;
@FXML
private TableColumn idCol;
@FXML
private TableColumn nameCol;
@FXML
private TableColumn ageCol;
@FXML
private TableColumn telCol;
...

public void loadCustomers() {

    idCol.setCellValueFactory(new MapValueFactory("cus_id"));
    nameCol.setCellValueFactory(new MapValueFactory("name"));
    ageCol.setCellValueFactory(new MapValueFactory("age"));
    telCol.setCellValueFactory(new MapValueFactory("telephone"));

    cusTable.setItems(getCustomerData());//Which returns a ObservableList<Map>   
   }