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

标签 java sql javafx

所以我尝试在表格 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/

相关文章:

java - Socket.io发射和监听器功能在Android上不起作用

javax.crypto.BadPaddingException : pad block corrupted

Java 继承和构造函数参数

java - 如何更新 JAXB 中的特殊元素?

mysql - 全短语优先搜索

php - 将数据库值从 html 表超链接传递到下一页的 SQL 查询

java - 比较 Oracle PACKAGE 中的日期类型不起作用

java - 如何显示黑底白字?

Swing 中的 Javafx 表

java - 运行 javafx 可执行 jar 文件时出现多个错误