Gabriel Mellides
Gabriel Mellides

Reputation: 5

Java FX TableView Database

I am building on Java FX application 3 weeks now and i have a problem i search things about that over the internet but i can't find the solution. I have to Populate Data on rows in a tableview i am getting data from database but i can't put it in the table i have this code that i use in order to take the data from Database.

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

How can I add Data in table cells from this ObservableList?

Upvotes: 0

Views: 806

Answers (1)

Madushan Perera
Madushan Perera

Reputation: 2598

It is good practice to have a separate class to CRUD operations:

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

You can implement your QueryClass like below :

public class QueryClass {

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

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

Lets assume the we have injected something like below to your controller from your 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>   
   }

Upvotes: 1

Related Questions