我试图将数据库表中的内容添加到TableView中,但是我得到了一个空表。
这是我尝试使用的Java代码。
我在表中使用了ID为txtdate,txthcd,txtgross,txtnet的4列。我的TableView的ID是表格。
package sample.Controllers;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import sample.Entities.MemberScreenModel;
import sample.Entities.PreviousRecordModel;
import sample.Records;
import sample.SqliteConnection;
import java.net.URL;
import java.sql.*;
import java.util.ResourceBundle;
public class PreviousRecordController implements Initializable {
MemberScreenModel model = new MemberScreenModel();
Connection connection;
ResultSet rs=null;
PreparedStatement ps=null;
java.sql.Date date;
float hcd;
int gross;
float net;
private String memberid =(new MemberScreenModel()).getMemberid();
public PreviousRecordController() {
connection = SqliteConnection.Connector();
if(connection == null)
System.exit(1);
}
@FXML private TableView<Records> table;
@FXML private TableColumn<Records, java.sql.Date> txtdate;
@FXML private TableColumn<Records, Float> txthcd;
@FXML private TableColumn<Records, Integer> txtgross;
@FXML private TableColumn<Records, Float> txtnet;
@FXML private Label txtmemid, txtname;
//private ObservableList<Records> records = FXCollections.observableArrayList();
@Override
public void initialize(URL location, ResourceBundle resources){
txtmemid.setText(model.getMemberid());
txtname.setText(model.getName());
txtdate.setCellValueFactory(new PropertyValueFactory<>("DATE"));
txthcd.setCellValueFactory(cellData -> cellData.getValue().getHCD().asObject());
txtgross.setCellValueFactory(cellData -> cellData.getValue().getGROSS().asObject());
txtnet.setCellValueFactory(cellData -> cellData.getValue().getNET().asObject());
try {
ObservableList<Records> records = getRecord();
table.setItems(records);
}
catch ( Exception e) {
System.out.println("Seting table: "+e);
}
//table.getColumns().addAll(txtdate, txthcd, txtgross, txtnet);
}
public ObservableList<Records> getRecord() throws SQLException, ClassNotFoundException {
table = new TableView<>();
String query = "Select `Date`, `HcDifferential`, `Gross`, `Net` from `PlayingData` where `Memberid`= ?";
try {
ps = connection.prepareStatement(query);
ps.setString(1, memberid);
rs = ps.executeQuery();
ObservableList<Records> records = getObjects(rs);
ps.close();
rs.close();
connection.close();
return records;
}
catch ( Exception e ) {
System.out.println("Error while extracting Records: " + e);
throw e;
}
}
public ObservableList<Records> getObjects(ResultSet rs) throws SQLException {
try {
ObservableList<Records> records = FXCollections.observableArrayList();
while (rs.next()) {
Records rec = new Records();
rec.setDate(rs.getDate("Date"));
rec.setHcd(Math.round(rs.getFloat("HcDifferential") *10.0f)/10.0f);
rec.setGross(rs.getInt("Gross"));
rec.setNet(rs.getFloat("Net"));
records.add(rec);
}
return records;
}
catch ( Exception e ) {
System.out.println("Exception in getObjects in PreviousRecord: "+e);
throw e;
}
}
}
Records类的代码为:
package sample;
import javafx.beans.property.FloatProperty;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.SimpleFloatProperty;
import javafx.beans.property.SimpleIntegerProperty;
import java.sql.Date;
public class Records {
private Date date;
private FloatProperty hcd;
private IntegerProperty gross;
private FloatProperty net;
public Records() {
//this.date = date;
this.hcd = new SimpleFloatProperty();
this.gross = new SimpleIntegerProperty();
this.net = new SimpleFloatProperty();
}
public void setDate(Date date) {
this.date = date;
}
public void setHcd(float hcd) {
this.hcd.set(hcd);
}
public void setGross(int gross) {
this.gross.set(gross);
}
public void setNet(float net) {
this.net.set(net);
}
public FloatProperty getHCD() {
return hcd;
}
public IntegerProperty getGROSS() {
return gross;
}
public FloatProperty getNET() {
return net;
}
public Date getDate() {
return date;
}
public Float getHcd() {
return hcd.get();
}
public Integer getGross() {
return gross.get();
}
public Float getNet() {
return net.get();
}
}
我没有收到任何异常,数据库连接设置正确,并且ResultSet能够获取所有值。我只能将这些值添加到TableView中。