Hello :)

I am working on a CRUD app to directly change a MySQL database on a remote server. I have a section where I can edit or delete entries. I choose an entry from a JList (populated by a ResultSet in a loop), and a JTextArea populates with the relevant info. Then there is 2 buttons, one to delete and one to update. Both work perfectly, my problem is: everytime I update or delete an entry, the JList gets "refreshed" by this:

private static void listRef() {
     try{
        model.clear(); // <----- DefaultListModel model = new DefaultListModel();

        String selectStatement = "SELECT * FROM test";
       PreparedStatement prepStmt = con.prepareStatement(selectStatement);
        ResultSet RS = prepStmt.executeQuery();
            int k = 0;
            while (RS.next()){
            model.add(k, RS.getString(1));
            k++;
            }
            
            RS.close();
            list.setSelectedIndex(0);
           }
        catch(Exception e){
         System.err.println("Exception: " + e.getMessage());   
        }
}

Problem is, it works, but inconsistently. I have tried my best to debug, but I just can't find out why it refreshes sometimes but not others :( I have tried putting in revalidate() and repaint(), to no avail.

The buttons' actions:
Delete:

try{
              String selectStatement = "DELETE FROM test WHERE ID = ?";
                PreparedStatement prepStmt = con.prepareStatement(selectStatement);
                prepStmt.setString(1, str); // <---- str is defined as index of the list
                prepStmt.executeUpdate();
                prepStmt.close();
                
            } catch(Exception e) {
      System.err.println("Exception: " + e.getMessage());
    }
     listRef(); //  <------ Refreshes JList
     
    }

Update:

try {
             
             String selectStatement = "UPDATE test SET datum = ?, boodskap = ? WHERE ID = ?";
                PreparedStatement prepStmt = con.prepareStatement(selectStatement);
                prepStmt.setString(1, editDate.getText());
                prepStmt.setString(2, editBoodskap.getText());
                prepStmt.setString(3, str);
                prepStmt.executeUpdate();
                prepStmt.close();
               
            } catch(Exception e) {
      System.err.println("Exception: " + e.getMessage());
    }
        listRef();
                     
    }

I hope this makes sense :S

Thank you.

:D! Perfect Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.