Ismail
Ismail

Reputation: 59

Delete a row from a JList

I am making a project and for this project I need to delete a specific row from a Jlist. The Jlist is filled with data from a database.

Now when I click my Jbutton I can delete a row from the column code, but when I want to delete a new row I need to change the query in the new code.

ActionPerformed:

@Override
    public void Delete() {
        Connection conn;
        String sql = "DELETE FROM OEFENINGEN WHERE CODE = '5'"; 

        try{
            conn = OpenConnection();
            stmt = conn.createStatement();

            int result = stmt.executeUpdate(sql);
            if(result > 0){
                System.out.println("Record Delete");
            } else{
                System.out.println("Record NOT Delete");
            }
        }catch (SQLException e){
            e.printStackTrace();
        } finally {
            if (stmt != null){
                try{
                    stmt.close();
                }catch (SQLException e){
                    e.printStackTrace();
                }  
            }
        }
        }   
    }

I want when you select a random row in the JList he delete the row from the database and Jlist when you click the Jbutton.

Upvotes: 0

Views: 677

Answers (1)

MadProgrammer
MadProgrammer

Reputation: 347204

Make sure that each row of the list is an actual Object that represents the data in the database (or is at least carrying the required information to be identifiable)

Create yourself an Action, pass a reference of the JList`ListModel` to it.

When the actionPerformed method is fired, check which rows are selected, extract the row data from the ListModel and call you delete method. In the delete method, use the information from the rows to identify criteria for the delete statement

Upvotes: 1

Related Questions