我正在尝试从数据库中选定的数据填充表视图。
下面代码中的一个片段
TableView<String> tableView = new TableView();
TableColumn<String, String> column1 = new TableColumn<String, String>("Customer Name");
column1.setCellValueFactory(new PropertyValueFactory<String, String>("Customer Name"));
TableColumn<String, String> column2 = new TableColumn<String, String>("Bill");
column2.setCellValueFactory(new PropertyValueFactory<String, String>("Bill"));
TableColumn<String, String> column3 = new TableColumn<String, String>("Phone Number");
column3.setCellValueFactory(new PropertyValueFactory<String, String>("Phone Number"));
try {
ObservableList<String> list = fill_table();
tableView.getItems().addAll(list);
} catch (ClassNotFoundException ex) {
Logger.getLogger(Restaurant_reservation.class.getName()).log(Level.SEVERE, null, ex);
}
tableView.getColumns().addAll(column1, column2, column3);
填表法
public ObservableList fill_table() throws SQLException, ClassNotFoundException {
ObservableList<String> details = null;
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/restaurant?serverTimezone=Europe/Amsterdam", "root", "123456");
Statement stmt = conn.createStatement();
String query = "SELECT customer_name,bill,phone_number FROM customer";
try {
ResultSet rs = stmt.executeQuery(query);
while (rs.next()) {
String Customer_Name = rs.getString("customer_name");
int Bill = rs.getInt("bill");
int PhoneNumber = rs.getInt("phone_number");
list.add(Customer_Name);
list.add(String.valueOf(Bill));
list.add(String.valueOf(PhoneNumber));
}
details = FXCollections.observableArrayList(list);
} catch (SQLException ex) {
Logger.getLogger(Restaurant_reservation.class.getName()).log(Level.SEVERE, null, ex);
}
return details;
}
当我打开sysout列表时,我得到了值,但是tableview仍然是空的。
暂无答案!
目前还没有任何答案,快来回答吧!