Как мне обновить personTableView
в ListPersonController
, когда я добавлю нового человека в AddPersonController
?
ListPersonController.java
public class ListPersonController {
@FXML
public TableView<Person> personTableView;
@FXML
private TableColumn<Person, String> name;
@FXML
private TableColumn<Person, String> gender;
@FXML
public void initialize() throws SQLException, ClassNotFoundException {
name.setCellValueFactory(new PropertyValueFactory<>("name"));
gender.setCellValueFactory(new PropertyValueFactory<>("gender"));
}
private List<Person> getAllPerson() throws ClassNotFoundException, SQLException {
Connection conn = DriverManager.getConnection("jdbc:sqlite:src/main/resources/person.db");
List<Person> personList = new ArrayList<>();
ResultSet rs;
try {
Class.forName("org.sqlite.JDBC");
Statement stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT id, name, gender FROM person");
while (rs.next()) {
String name = rs.getString("name");
String gender = rs.getString("gender");
personList.add(new Person(name, gender));
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return personList;
}
}
AddPersonController.java
public class AddPersonController {
@FXML
protected void handleSaveButtonAction(ActionEvent event) throws SQLException, ClassNotFoundException {
String fName = "John";
String fGender = "female"
Person person = new Person(
fName,
fGender,
);
save(person);
}
private void insertIntoPersonTable(Person person) throws ClassNotFoundException, SQLException {
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:src/main/resources/person.db");
PreparedStatement prep = conn.prepareStatement("INSERT INTO person VALUES (?, ?);");
prep.setString(2, person.getName());
prep.setString(3, person.getCommonName());
prep.addBatch();
conn.setAutoCommit(false);
prep.executeBatch();
conn.setAutoCommit(true);
}
}