JavaFX ObservableArrayList无输出

JavaFX ObservableArrayList无输出,javafx,tableview,Javafx,Tableview,我很难在JavaFX的TableView上打印出一个observableArrayList。代码上没有出现错误,也没有给我任何具体的工作。代码将运行,但不会出现输出。我使用outprint进行检查,它似乎在try语句之前停止。这是我第一次与FX合作,因此任何帮助都将不胜感激 应用程序类: import javafx.application.Application; import static javafx.application.Application.launch; import javafx

我很难在JavaFX的TableView上打印出一个observableArrayList。代码上没有出现错误,也没有给我任何具体的工作。代码将运行,但不会出现输出。我使用outprint进行检查,它似乎在try语句之前停止。这是我第一次与FX合作,因此任何帮助都将不胜感激

应用程序类:

import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.scene.Scene;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Main extends Application {

    Stage window;
    TableView<Product> table;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        window = primaryStage;
        window.setTitle("List");
        Connection connection = getConnection();

        //Name column
        TableColumn<Product, String> code = new TableColumn<>("code");
        code.setMinWidth(200);
        code.setCellValueFactory(new PropertyValueFactory<>("code"));

        //Price column
        TableColumn<Product, Double> price = new TableColumn<>("Price");
        price.setMinWidth(100);
        price.setCellValueFactory(new PropertyValueFactory<>("price"));

        //Quantity column
        TableColumn<Product, String> description = new TableColumn<>("description");
        description.setMinWidth(100);
        description.setCellValueFactory(new PropertyValueFactory<>("description"));
        getConnection(); 
        table = new TableView<>();
        table.setItems(getProduct());
        table.getColumns().addAll(code, description, price);

        VBox vBox = new VBox();
        vBox.getChildren().addAll(table);

        Scene scene = new Scene(vBox);
        window.setScene(scene);
        window.show();
    }

    private Connection getConnection() throws SQLException {
        String dbUrl = "jdbc:sqlite:products.sqlite";
        Connection connection = DriverManager.getConnection(dbUrl);
        return connection;
    }

    //Get all of the products
    public ObservableList<Product> getProduct(){
        String sql = "SELECT ProductCode, Description, Price "
                  + "FROM Products ORDER BY ProductCode ASC";
        ObservableList<Product> products = FXCollections.observableArrayList();
        try (Connection connection = getConnection();
             PreparedStatement ps = connection.prepareStatement(sql);
             ResultSet rs = ps.executeQuery()) {          
            while (rs.next()) {
                String code = rs.getString("ProductCode");
                String description = rs.getString("Description");
                double price = rs.getDouble("Price");

                Product p = new Product(code, description, price);
                products.add(p);
            }

        } catch (SQLException e) { 
        }
        return products;
    }
}
import java.text.NumberFormat;

public class Product {

    private String name;
    private double price;
    private int quantity;
    private String code;
    private String description;

    public Product(){
        this.code = "";
        this.description = "";
        this.price = 0;
    }

    public Product(String code, double price, String description){
        this.code = code;
        this.price = price;
        this.description = description;
    }

    public Product(String code, String description, double price) {
        this.code = code;
        this.description = description;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDescription() {
        return description;
    }

    public String getPriceFormatted() {
        NumberFormat currency = NumberFormat.getCurrencyInstance();
        return currency.format(price);
    } 
}

您说没有出现错误,但似乎您正在接受任何可能的
SQLException
。如果在代码中抛出一个
SQLException
,唯一的效果就是返回一个空的或不完整的
ObservableList
。至少在
catch
块中添加一个
e.printStackTrace()
,然后再次尝试运行它以查看是否出现任何错误。另外,在
start
方法中调用
getConnection()
两次,但从不使用返回的
连接(甚至不关闭它们);您只需将返回的
连接
分配给变量一次。你想在那里做什么?添加了e.printStackTrace()并得到一个错误块。它告诉我我的桌子产品不存在。这就是进步。至于多连接,我认为db没有连接,如果是这样的话,它试图引发错误消息。至少我知道现在的问题是什么。非常感谢。