gpt4 book ai didi

mysql - 我的表格 View 显示空白行

转载 作者:行者123 更新时间:2023-11-29 21:49:36 25 4
gpt4 key购买 nike

我是 JavaFX 新手,我一直在尝试实现此代码以在 TableView 中显示 MySQL 数据库中的数据。问题是,当我运行代码时,我得到空白行,但我不知道为什么。

这是我的类(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;
}
}
}

最佳答案

假设您的数据库查询有效,则属性名称错误:

如果属性“getter”名为 myNameProperty(),则需要使用 new PropertyValueFactory("myName")

需要重命名属性“getters”或与值工厂一起使用的字符串:

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

关于mysql - 我的表格 View 显示空白行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33775647/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com