0

我正在尝试显示存储在我的数据库(sqlite)中的数据,我可以检索数据,但它会在它之前显示它的数据类型!例如,它不显示“1”,而是显示“IntegerProperty [Value: 1]”

在此处输入图像描述

这是数据库的图片(称为“记录”的表): 在此处输入图像描述

这是项目结构的图片: 在此处输入图像描述

这是控制器类:


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

import db.dbConnection;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;

public class mainController implements Initializable {

    @FXML
    private TableColumn<RecordData, String> ncol;

    @FXML
    private TableColumn<RecordData, Double> pcol;

    @FXML
    private TextField artist;

    @FXML
    private TextField year;

    @FXML
    private TextField rating;

    @FXML
    private TableColumn<RecordData, String> gcol;

    @FXML
    private TableColumn<RecordData, String> acol;

    @FXML
    private TableColumn<RecordData, Integer> idcol;

    @FXML
    private TextField price;

    @FXML
    private TextField name;

    @FXML
    private TextField genre;

    @FXML
    private TableColumn<RecordData, Integer> rcol;

    @FXML
    private TextField id;

    @FXML
    private TableView<RecordData> recordTable;

    @FXML
    private TableColumn<RecordData, Integer> ycol;

    private db.dbConnection connection;
    private ObservableList<RecordData> data;

    @FXML
    void add(ActionEvent event) {
        String sqlin = "INSERT INTO Records(ID,Name,Artist,Year,Genre,Price,Rating) VALUES(?,?,?,?,?,?,?)";
        try {
            Connection conn = connection.getConnection();
            PreparedStatement ps = conn.prepareStatement(sqlin);
            ps.setInt(1, Integer.parseInt(this.id.getText()));
            ps.setString(2, this.name.getText());
            ps.setString(3, this.artist.getText());
            ps.setInt(4, Integer.parseInt(this.year.getText()));
            ps.setString(5, this.genre.getText());
            ps.setDouble(6, Double.parseDouble(this.price.getText()));
            ps.setInt(7, Integer.parseInt(this.rating.getText()));
            ps.execute();
            conn.close();
        }
        catch(Exception e) {
            e.printStackTrace();
        }
    }

    @FXML
    void clear(ActionEvent event) {

    }

    @FXML
    void load(ActionEvent event) {
        try {
            Connection conn = connection.getConnection();
            this.data = FXCollections.observableArrayList();
            ResultSet rs = conn.createStatement().executeQuery("SELECT * FROM Records");
            while(rs.next()) {
                this.data.add(new RecordData(rs.getInt(1),rs.getString(2),rs.getString(3),rs.getInt(4),rs.getString(5),rs.getDouble(6),rs.getInt(7)));
            }
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        this.idcol.setCellValueFactory(new PropertyValueFactory<RecordData, Integer>("ID"));
        this.ncol.setCellValueFactory(new PropertyValueFactory<RecordData, String>("Name"));
        this.acol.setCellValueFactory(new PropertyValueFactory<RecordData, String>("Artist"));
        this.ycol.setCellValueFactory(new PropertyValueFactory<RecordData, Integer>("Year"));
        this.gcol.setCellValueFactory(new PropertyValueFactory<RecordData, String>("Genre"));
        this.pcol.setCellValueFactory(new PropertyValueFactory<RecordData, Double>("Price"));
        this.rcol.setCellValueFactory(new PropertyValueFactory<RecordData, Integer>("Rating"));

        this.recordTable.setItems(null);
        this.recordTable.setItems(this.data);
    }

    public void initialize(URL url, ResourceBundle rb ) {
        this.connection = new dbConnection();
    }

}

这是记录数据类:


import javafx.beans.property.DoubleProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleDoubleProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;

public class RecordData {

    private IntegerProperty ID;
    private StringProperty Name;
    private StringProperty Artist;
    private IntegerProperty Year;
    private StringProperty Genre;
    private DoubleProperty Price;
    private IntegerProperty Rating;

    public RecordData(int id, String name, String artist, int year, String genre, double price, int rating) {
        this.ID = new SimpleIntegerProperty(id);
        this.Name = new SimpleStringProperty(name);
        this.Artist = new SimpleStringProperty(artist);
        this.Year = new SimpleIntegerProperty(year);
        this.Genre = new SimpleStringProperty(genre);
        this.Price = new SimpleDoubleProperty(price);
        this.Rating = new SimpleIntegerProperty(rating);


    }

    public IntegerProperty getID() {
        return ID;
    }

    public void setID(IntegerProperty iD) {
        ID = iD;
    }

    public StringProperty getName() {
        return Name;
    }

    public void setName(StringProperty name) {
        Name = name;
    }

    public StringProperty getArtist() {
        return Artist;
    }

    public void setArtist(StringProperty artist) {
        Artist = artist;
    }

    public IntegerProperty getYear() {
        return Year;
    }

    public void setYear(IntegerProperty year) {
        Year = year;
    }

    public StringProperty getGenre() {
        return Genre;
    }

    public void setGenre(StringProperty genre) {
        Genre = genre;
    }

    public DoubleProperty getPrice() {
        return Price;
    }

    public void setPrice(DoubleProperty price) {
        Price = price;
    }

    public IntegerProperty getRating() {
        return Rating;
    }

    public void setRating(IntegerProperty rating) {
        Rating = rating;
    }


如果有人知道如何获取值而不是“IntegerProperty [Value: 1]”部分,请帮助我!

4

1 回答 1

0

a PropertyValueFactorywith <name>provided as constructor 参数的工作方式是:

  1. 它试图<name>Property()在项目类中找到一个方法,返回一个ObservableValue
  2. 如果没有找到这样的方法,它会尝试找到一个方法get<Name>()并将返回的值包装在一个属性对象中

由于您的RecordData类不遵守 javafx 属性的约定,因此使用了第二种选择,但不是获取应该包装在ObservableValue对象中的值,而是返回的对象是属性本身,它们PropertyValueFactory继续包装在属性对象中导致in ObservableValue<StringProperty>s 等被退回,而不是等ObservableValue<String>

为属性提供 getter 和 setter 违背了目的顺便说一句:它们对于使用通过添加的侦听器观察值的变化很有用addListener。如果您替换整个属性而不是分配新值,则不会触发侦听器。

这是year应该如何重写属性的方法。相应地修改其他属性:

private final IntegerProperty year; // prevent accidental replacement and adhere to java naming conventions
...

public RecordData(int id, String name, String artist, int year, String genre, double price, int rating) {
    ...
    this.year = new SimpleIntegerProperty(year);
    ...
}

public int getYear() {
    return year.get();
}

public void setYear(int value) {
    year.set(value);
}

public IntegerProperty yearProperty() {
    return year;
}
this.ycol.setCellValueFactory(new PropertyValueFactory<RecordData, Integer>("year"));

您也可以用PropertyValueFactorylambda 表达式替换使用。这将允许编译器进行一些类型检查:

this.ycol.setCellValueFactory(cellData -> cellData.getValue().yearProperty());

请注意,这IntegerProperty不需要ObservableValue<Number>ObservableValue<Integer>修改列的声明。

于 2019-08-23T08:18:01.893 回答