0

I have a JavaFX project I've been working on for a few days. I have a database with a few tables that I, on startup of the program, obtain specific fields from and place in objects. Countries, Customers,etc etc. I gather all this data in my main() thread with Manager.loadData(); after connecting to my database via JDBC driver.

When attempting to modify a selected object, for instance Customers, I load a seperate fxml where I display current values of the object in textfields where I can modify them. After modifying, I have a Save button that onAction looks like this

    @FXML
    private void onActionSaveCustomer(ActionEvent event) throws IOException {
        
        try{
            DBQuery.setStatement(DBConnection.getConnection());
            Statement statement = DBQuery.getStatement();
        
            String updateStatement = "UPDATE customers SET "
               + "Customer_Name = '" + nameTextField.getText()
                + "', Address = '" + addressTextField.getText()
                + "', Postal_Code = '" + postalTextField.getText()
                + "', Phone = '" + phoneTextField.getText()
                + "' WHERE Customer_ID = '" + customerTextField.getText() + "'";
        
            statement.execute(updateStatement);
        } catch(SQLException e) {
            System.out.println(e);
        }
        
        stage = (Stage)((Button)event.getSource()).getScene().getWindow();
        scene = FXMLLoader.load(getClass().getResource("/view/SchedulingHome.fxml"));
        stage.setScene(new Scene(scene));
        stage.show();
 
    }


The UPDATE SQL statement works just fine, no worries there. When switching over to /view/SchedulingHome.fxml, I just can't seem to figure out how to/where to run my reloadData() method that will delete all instances of my object and then redownload them from my database WITHOUT incurring a ConcurrentModificationException

private void reloadData() throws SQLException {
        Manager.deleteData();
        Manager.loadData();
    }

My error occurs right on Manager.deleteData() as I attempt to call reloadData() in my SchedulingHomeController initialize() where I am at the same time iterating over my objects and filling a tableview with attributes from these objects. I'm just not sure where I can delete my data where I'm not also iterating over it? I iterate over it in my modify screen and I iterate over it on my next screen. Is there some sort of limbo async area I can run reloadData() where I'm not iterating over the objects it's trying to delete?

  • 1
    It's hard to tell without a [mre] in your question, but your SQL should be separated into its own class. You should be reading into a java.util.List of objects and writing from a java.util.List of Objects. You would then populate your scenes and stages from your Lists. You should also be using Java PreparedStatements to avoid SQL injection problems. – Gilbert Le Blanc Sep 16 '21 at 04:59
  • 1
    Either you are concurrently modifying a list on different threads or modifying a list you are currently iterating over in a single thread (I don’t know which). Likely it is a single thread, as JavaFX is, by nature, single threaded. Unless you create threads (you don’t do you?). There are [many ways to eliminate a concurrent exception while removing data](https://stackoverflow.com/questions/10431981/remove-elements-from-collection-while-iterating). – jewelsea Sep 16 '21 at 05:37
  • @jewelsea that link really explained and helped me fix my problem. I appreciate your help! – Tyler Quante Sep 16 '21 at 15:32

0 Answers0