简体   繁体   English

从JList删除一行

[英]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. 我正在制作一个项目,对于这个项目,我需要从Jlist中删除特定的行。 The Jlist is filled with data from a database. Jlist充满了来自数据库的数据。

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. 现在,当我单击我的Jbutton时,可以从列代码中删除一行,但是当我想删除新行时,需要在新代码中更改查询。

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. 我想要当您在JList中选择一个随机行时,当您单击Jbutton时,他将从数据库和Jlist中删除该行。

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. 创建你自己的Action ,传递的参考JList吧`ListModel`。

When the actionPerformed method is fired, check which rows are selected, extract the row data from the ListModel and call you delete method. 触发actionPerformed方法后,检查选择了哪些行,从ListModel提取行数据,然后调用delete方法。 In the delete method, use the information from the rows to identify criteria for the delete statement 在delete方法中,使用行中的信息来标识delete语句的条件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM