Мой код должен загрузить представление таблицы клиента в javafx через loadCusotmerTable()
из базы данных MYSQL с JDB C через метод CustomerDOA.findAll()
. Метод findAll()
возвращает ObservableList, который должен отображать клиентов.
В выходных данных таблицы в настоящий момент отображается правильное количество строк для каждого клиента в списке Observable, но отображаются только данные из последней записи в ObservableList. Что-то отсутствует в моем коде для правильной загрузки ObservableList<Customer> Customers
?
loadCustomerTable
package Controller;
import Model.Customer;
import Model.DAO.CustomerDAO;
import Model.DAO.UserDAO;
import Model.DBConnection;
import java.io.IOException;
import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.ResourceBundle;
import java.util.Set;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.MenuButton;
import javafx.scene.control.MenuItem;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextField;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.stage.Stage;
public class CustomerViewController implements Initializable {
@FXML private TextField TextFieldCustomerID;
@FXML private TextField TextFieldCustomerName;
@FXML private Button ButtonGoBack;
@FXML private Button ButtonAddModify;
@FXML private Button ButtonRemove;
@FXML private Button ButtonCancel;
@FXML private MenuButton MenuButtonAddress;
@FXML private MenuButton MenuButtonActive;
@FXML private MenuItem MenuActiveItemActive;
@FXML private MenuItem MenuActiveItemInactive;
@FXML private TableView<Customer> TableViewCustomer;
@FXML private TableColumn<Customer, String> TableCustomerColumnCustomerID;
@FXML private TableColumn<Customer, String> TableCustomerColumnCustomerName;
@FXML private TableColumn<Customer, String> TableCustomerColumnAddress;
@FXML private TableColumn<Customer, String> TableCustomerColumnActive;
private int addressId = 1;
private int Active = 1;
private int userId = 1;
private boolean isCustomerSelected = true;
private ObservableList<Customer> Customers = FXCollections.observableArrayList();
// .......
public void loadCustomerTable(){
CustomerDAO customerDAO = new CustomerDAO(DBConnection.getConnection());
ObservableList<Customer> Customers = customerDAO.findAll();
TableCustomerColumnCustomerID.setCellValueFactory(new PropertyValueFactory<>("customerId"));
TableCustomerColumnCustomerName.setCellValueFactory(new PropertyValueFactory<>("customerName"));
TableCustomerColumnAddress.setCellValueFactory(new PropertyValueFactory<>("addressId"));
TableCustomerColumnActive.setCellValueFactory(new PropertyValueFactory<>("active"));
TableViewCustomer.setItems(Customers);
}
CustomerDAO.findAll ()
public ObservableList<Customer> findAll() {
ObservableList<Customer> Customers = FXCollections.observableArrayList();
Customer customer = new Customer();
try(PreparedStatement statement = this.connection.prepareStatement(GET_ALL)){
ResultSet resultSet = statement.executeQuery();
int i =0;
while(resultSet.next()){
customer.setCustomerId(resultSet.getInt("customerId"));
customer.setCustomerName(resultSet.getString("customerName"));
customer.setAddressId(resultSet.getInt("addressId"));
customer.setActive(resultSet.getInt("active"));
customer.setCreateDate(resultSet.getTimestamp("createDate"));
customer.setLastUpdate(resultSet.getTimestamp("lastUpdate"));
customer.setCreatedBy(resultSet.getString("createdBy"));
customer.setLastUpdateBy(resultSet.getString("lastUpdateBy"));
Customers.add(customer);
System.out.println(Customers.get(i).getCustomerName());
i++;
}
}catch(SQLException e){
e.printStackTrace();
throw new RuntimeException(e);
}
return Customers;
}
Класс клиента
import Utils.DataTransferObject;
import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import javafx.beans.property.IntegerProperty;
public class Customer implements DataTransferObject {
private int customerId;
private String customerName; //varchar 45
private int addressId;
private int active; // tiny int 1
private LocalDateTime createDate;
private LocalDateTime lastUpdate;
private String createdBy;
private String lastUpdateBy;
@Override
public long getId() {
return customerId;
}
public int getCustomerId() {
return customerId;
}
public void setCustomerId(int customerId) {
this.customerId = customerId;
}
public String getCustomerName() {
return customerName;
}
public void setCustomerName(String customerName) {
this.customerName = customerName;
}
public int getAddressId() {
return addressId;
}
public void setAddressId(int addressId) {
this.addressId = addressId;
}
public int getActive() {
return active;
}
public void setActive(int active) {
this.active = active;
}
public LocalDateTime getCreateDate() {
return createDate;
}
public void setCreateDate(LocalDateTime createDate) {
this.createDate = createDate;
}
public void setCreateDate(Timestamp createDate) {
this.createDate = createDate.toLocalDateTime();
}
public LocalDateTime getLastUpdate() {
return lastUpdate;
}
public void setLastUpdate(LocalDateTime lastUpdate) {
this.lastUpdate = lastUpdate;
}
public void setLastUpdate(Timestamp lastUpdate) {
this.lastUpdate = lastUpdate.toLocalDateTime();
}
public String getCreatedBy() {
return createdBy;
}
public void setCreatedBy(String createdBy) {
this.createdBy = createdBy;
}
public String getLastUpdateBy() {
return lastUpdateBy;
}
public void setLastUpdateBy(String lastUpdateBy) {
this.lastUpdateBy = lastUpdateBy;
}
}