我正在尝试显示存储在我的数据库(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]”部分,请帮助我!