0

I am new to JavaFX and I have been trying to implement this code for displaying data from MySQL database in a TableView. The problem is that when I run the code I get blank rows and I don't know why.

Here is my code for the class:

import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.time.ZoneId;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.ResourceBundle;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.ComboBox;
import javafx.scene.control.DatePicker;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.AnchorPane;
import javafx.stage.Stage;
import javax.swing.JOptionPane;

public class FXMLPatientsMasterController implements Initializable {

    /*
       mysql connection variables
    */
    Connection conn=null;
    PreparedStatement pat=null;
    ResultSet rs=null;
    private ObservableList<PatientDetails> patients;

     @FXML
     private TableView Table_Patients;

     @FXML
     private TableColumn patientID;

     @FXML
     private TableColumn Name;

     @FXML
     private TableColumn Surname;

     @FXML
     private TableColumn pnationalID;

     @FXML
     private TableColumn psex;

     @FXML
     private TableColumn pDOB;

      // Some code here

    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        conn = PMS263MySqlConnection.ConnectDB();

        try {                       
            patients = FXCollections.observableArrayList();
            rs = conn.createStatement().executeQuery("select * from     patients");
            while (rs.next()) {
                patients.add(new PatientDetails(rs.getInt("PatientId"),
                rs.getString("fName"), rs.getString("Surname"), rs.getString("National_ID"), rs.getString("Sex"), rs.getString("DOB")));
            }
            patientID.setCellValueFactory(new PropertyValueFactory("PatientId"));
            Name.setCellValueFactory(new PropertyValueFactory("fName"));
            Surname.setCellValueFactory(new PropertyValueFactory("Surname"));
            pnationalID.setCellValueFactory(new PropertyValueFactory("National_ID"));
            psex.setCellValueFactory(new PropertyValueFactory("Sex"));
            pDOB.setCellValueFactory(new PropertyValueFactory("DOB"));
            Table_Patients.setItems(null);
            Table_Patients.setItems(patients);         
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Error on Building Data");
        }
    }

    public static class PatientDetails {
        private final SimpleIntegerProperty Patientid;
        private final StringProperty Pname;
        private final StringProperty Psurname;
        private final StringProperty Pnationalid;
        private final StringProperty Psex;
        private final StringProperty Pdob;
        private PatientDetails(int patientid, String pname, String psurname, String pnationalid, String psex, String pdob) {
            this.Patientid = new SimpleIntegerProperty(patientid);
            this.Pname = new SimpleStringProperty(pname);
            this.Psurname = new SimpleStringProperty(psurname);
            this.Pnationalid = new SimpleStringProperty(pnationalid);
            this.Psex= new SimpleStringProperty(psex);
            this.Pdob = new SimpleStringProperty(pdob);
        }

        public SimpleIntegerProperty patientidProperty() {
            return Patientid ;
        }

        public StringProperty pnameProperty() {
            return Pname;
        }

        public StringProperty psurnameProperty() {
            return Psurname;
        }

        public StringProperty pnationalidProperty() {
            return Pnationalid;
        }

        public StringProperty psexPsexProperty() {
            return Psex;
        }
        public StringProperty pdobProperty() {
            return Pdob;
        }
    }    
}    
fabian
  • 80,457
  • 12
  • 86
  • 114
Sali
  • 3
  • 2
  • Please read the [PropertyValueFactory](https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/cell/PropertyValueFactory.html) – Uluk Biy Nov 18 '15 at 11:29

1 Answers1

0

Assuming your database query works, you the property names wrong:

If the property "getter" is named myNameProperty() you need to use new PropertyValueFactory("myName").

Either the property "getters" need to be renamed or the strings used with the value factories:

patientID.setCellValueFactory(new PropertyValueFactory("patientId"));
Name.setCellValueFactory(new PropertyValueFactory("pname"));
Surname.setCellValueFactory(new PropertyValueFactory("psurname"));
pnationalID.setCellValueFactory(new PropertyValueFactory("pnationalid"));
psex.setCellValueFactory(new PropertyValueFactory("psex"));
pDOB.setCellValueFactory(new PropertyValueFactory("pdob"));

...

    public SimpleIntegerProperty patientIdProperty() {
        return Patientid;
    }

    public StringProperty pnameProperty() {
        return Pname;
    }

    public StringProperty psurnameProperty() {
        return Psurname;
    }

    public StringProperty pnationalidProperty() {
        return Pnationalid;
    }

    public StringProperty psexProperty() {
        return Psex;
    }

    public StringProperty pdobProperty() {
        return Pdob;
    }
fabian
  • 80,457
  • 12
  • 86
  • 114