gpt4 book ai didi

即使加载数据,JavaFx Tableview 数据库也不可见

转载 作者:太空宇宙 更新时间:2023-11-04 09:18:23 24 4
gpt4 key购买 nike

所以我尝试在表格 View 中显示一些数据库值几个小时,起初我无法显示任何内容,但是在了解 PropertyValue 与 thh getters 一起使用并实现它们之后,我可以在表中显示我的数据,但问题是有些数据没有显示,我真的不明白为什么我完整地查看了我的代码但找不到错误,也许有人可以给我指点。

在图片中,您可以看到我得到的结果以及我的值在 sql 中的数据类型

Controller 类

package Controllers;

import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ResourceBundle;

import Model.Datenbank;
import Model.flightRouteAddModel;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;

public class FlightRouteOverview implements Initializable {

public static Connection connection;
public static Statement statement;
public Datenbank datenbank;
overviewAirplaneController controller;

@FXML
private TableColumn<flightRouteAddModel, String> startAirport;

@FXML
private TableColumn<flightRouteAddModel, String> targetAirport;

@FXML
private TableColumn<flightRouteAddModel, String> flightDate;

@FXML
private TableColumn<flightRouteAddModel, String> airplane;

@FXML
private TableColumn<flightRouteAddModel, Integer> intervall;

@FXML
private TableColumn<flightRouteAddModel, Integer> seatCountEco;

@FXML
private TableColumn<flightRouteAddModel, Integer> seatCountBus;

@FXML
private TableColumn<flightRouteAddModel, Float> priceEco;

@FXML
private TableColumn<flightRouteAddModel, Float> priceBus;

@FXML
private TableColumn<flightRouteAddModel, Float> distance;

@FXML
private TableView<flightRouteAddModel> table;

public FlightRouteOverview() {


}

static ObservableList<flightRouteAddModel> flightList = FXCollections.observableArrayList();
@Override
public void initialize(URL location, ResourceBundle resourced) {


try {
verbinden();

// Ablesen
Statement pStatement = connection.createStatement();
ResultSet myRs = pStatement.executeQuery("select * from fluglinie");
while (myRs.next()) {
flightList.add(new flightRouteAddModel(myRs.getString("startFlughafen"), myRs.getString("zielFlughafen"),
myRs.getString("startDatum"), myRs.getInt("intervall"), myRs.getString("flugzeug"),
myRs.getInt("anzahlEconomy"), myRs.getInt("anzahlBusiness"), myRs.getFloat("preisEconomy"),
myRs.getFloat("preisBusines"), myRs.getFloat("distanz")));


}

} catch (Exception e) {
System.out.println(e);

}


startAirport.setCellValueFactory(new PropertyValueFactory<>("startAirport"));
targetAirport.setCellValueFactory(new PropertyValueFactory<>("targetAirport"));

flightDate.setCellValueFactory(new PropertyValueFactory<>("flightDate"));
airplane.setCellValueFactory(new PropertyValueFactory<>("airplane"));
intervall.setCellValueFactory(new PropertyValueFactory<>("intervall"));
seatCountEco.setCellValueFactory(new PropertyValueFactory<>("seatCountEco"));
seatCountBus.setCellValueFactory(new PropertyValueFactory<>("seatCountBus"));
priceEco.setCellValueFactory(new PropertyValueFactory<>("priceEco"));
priceBus.setCellValueFactory(new PropertyValueFactory<>("priceBus"));
distance.setCellValueFactory(new PropertyValueFactory<>("distance"));

table.setItems(flightList);


}

public void verbinden() throws SQLException, ClassNotFoundException {
// TODO Auto-generated method stub
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/flugverwaltung?autoReconnect=true&"
+ "useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=Europe/Berlin", "root", "passwort");

Class.forName("com.mysql.cj.jdbc.Driver");

}

}

模型类

package Model;

import javafx.beans.property.FloatProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleFloatProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class flightRouteAddModel {

public StringProperty startAirport;
public StringProperty targetAirport;
public StringProperty flightDate;
public IntegerProperty intervall;
public StringProperty airPlane;
public IntegerProperty countEconomy;
public IntegerProperty countBusiness;
public FloatProperty priceEconomy;
public FloatProperty priceBusiness;
public FloatProperty distance;

public flightRouteAddModel(String startAirport, String targetAirport, String flightDate,Integer intervall,
String airPlane, Integer countEconomy, Integer countBusiness, Float priceEconomy,
Float priceBusiness, Float distance) {
super();

this.startAirport = new SimpleStringProperty(startAirport);
this.targetAirport = new SimpleStringProperty(targetAirport);
this.flightDate = new SimpleStringProperty(flightDate);
this.intervall = new SimpleIntegerProperty(intervall);
this.airPlane = new SimpleStringProperty(airPlane);
this.countEconomy = new SimpleIntegerProperty(countEconomy);
this.countBusiness = new SimpleIntegerProperty(countBusiness);

this.priceEconomy = new SimpleFloatProperty(priceEconomy);
this.priceBusiness = new SimpleFloatProperty(priceBusiness);
this.distance = new SimpleFloatProperty(distance);


}

public String getStartAirport() {
return startAirport.get();
}

public String getTargetAirport() {
return targetAirport.get();
}

public String getFlightDate() {
return flightDate.get();
}

public Integer getIntervall() {
return intervall.get();
}

public String getAirPlane() {
return airPlane.get();
}

public Integer getCountEconomy() {
return countEconomy.getValue();
}

public Integer getCountBusiness() {
return countBusiness.get();
}

public Float getPriceEconomy() {
return priceEconomy.get();
}

public Float getPriceBusiness() {
return priceBusiness.get();
}

public Float getDistance() {
return distance.get();
}

}

Result

DatabseType

最佳答案

尝试设置 CellFactory .setCellFactory()在列上

但这可能不是问题所在。我猜想 PropertyValueFactory 期望属性是字符串而不是 StringProperties。自己制作一个:

startAirport.setCellValueFactory(param-> param.getValue().startAirport);

关于即使加载数据,JavaFx Tableview 数据库也不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58665826/

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