Display Items from a Database in a JavaFX TableView

javafx, list, println, resultset, tableview

Solution

Your Issue

Your table is empty because you never initialize the values of the `dataClass roww` which you create. Try the following code instead:

dataClass roww = new dataClass();
roww.setKey(Key);
roww.setSalesNo(SalesNo);
ll.add(roww);

An even better idea, may be to provide a constructor for the dataClass which takes the `Key` and `SalesNo` as parameters - that way your `dataClass` instances are guaranteed to be correctly initialized.

ll.add(
  new dataClass(
    Key, 
    SalesNo
  )
);

Reference Solution

Here's a link to some sample code for accessing a database from JavaFX via JDBC and populating a `ListView` with results. The code was created for the StackOverflow question: javafx connection to mysql.

That code, similar to the code in your question, is simple to understand (which is why I linked it), but suffers from a flaw that the database lookup is done on the JavaFX application thread. This means that the application UI is frozen while the database lookup occurs. That is not optimal. To fix that, I created another background thread based database access sample for the stackoverflow question: JavaFX - Background Thread for SQL Query. This solution, which executes queries on a background thread using a `Task`, is preferable.

Aside

I'd advise following Java naming conventions as it will make your programs easier to be understood by other developers.

Problem

Ok, so I'm new to this, and I've been searching for two weeks trying to get an SQL resultset to print to a TableView made by JavaFX Scene Builder. I can print my resultset using System.out.println and the data is correct, but when I try put it into a List, my tableview shows one column with 0s and the other column blank. I'm trying to print the List (println), but I get the object pointer(?) and not the data, so I can't see if I'm passing from the ResultSet to the List incorrectly. Any help would be appreciated ... I want to understand the concept, not just get an answer. Here's my controller code: ``` public class TesterUIController implements Initializable { @FXML private TableView<dataClass> Table; @FXML private Button TestButton; @FXML private TableColumn<dataClass, Integer> colKey; @FXML private TableColumn<dataClass, String> colSalesNo; public static void main (String[] args) { System.out.println("Main has run"); } /** * * @param fxmlFileLocation * @param resources */ @Override public void initialize(URL fxmlFileLocation, ResourceBundle resources) { TestButton.setOnAction(new EventHandler<ActionEvent>() { @Override public void handle(ActionEvent event) { System.out.println("Button Pressed"); colKey.setCellValueFactory(new PropertyValueFactory<dataClass, Integer>("Key")); colSalesNo.setCellValueFactory(new PropertyValueFactory<dataClass, String>("SalesNo")); Table.getItems().setAll(gobbledyGook()); } } ); } /* * dataClass is a (sub)class representing the data for TableView "Table" */ public class dataClass { private IntegerProperty Key; public void setKey (int value) {KeyProperty().set(value);} public int getKey() { return KeyProperty().get(); } public IntegerProperty KeyProperty() { if (Key == null) Key = new SimpleIntegerProperty(this, "Key"); return Key; } private StringProperty SalesNo; public void setSalesNo(String value) { SalesNoProperty().set(value); } public String getSalesNo() { return SalesNoProperty().get(); } public StringProperty SalesNoProperty() { if (SalesNo == null) SalesNo = new SimpleStringProperty(this, "SalesNo"); return SalesNo; } } private List<dataClass> gobbledyGook() { //ObservableList<dataClass> gobbledyGook() { // ResultSet getData() { Connection conn; String dbDriver; Statement st; ResultSet rs;// = null; List ll = new LinkedList(); try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); dbDriver = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};" + "DBQ=Inventario.mdb;"; conn = DriverManager.getConnection(dbDriver, "", ""); st = conn.createStatement(); String getSalesNumber = "SELECT * FROM SalesNumber " // TOP 1 * FROM ... + "ORDER BY SalesNumber.Key DESC";// LIMIT 1"; rs = st.executeQuery(getSalesNumber); while (rs.next()) { int Key = rs.getInt(1); double saleNo = rs.getDouble(2); NumberFormat formatter = new DecimalFormat("###########"); String SalesNo = formatter.format(saleNo); System.out.println(Key + ", " + SalesNo); //key + ", " + saleNo); dataClass roww = new dataClass(); ll.add(roww); } } catch (ClassNotFoundException | SQLException ex) { Logger.getLogger(TesterUIController.class.getName()).log(Level.SEVERE, null, ex); } return ll; } } ```

Original source

Related problems