【问题标题】:Not able to clear the previous table data无法清除之前的表格数据
【发布时间】:2013-04-13 03:04:52
【问题描述】:

我制作了一个带有搜索引擎功能的小程序。这里的问题是,当我尝试首先使用关键字进行搜索时,通过以 JTable 格式打开一个新的 JFrame 来正确显示它。但是当我关闭该框架并在搜索表单中输入其他内容以搜索其他内容时,它不仅会显示我搜索过的最新内容,还会显示我之前搜索过的内容。在水平方向上,它也不断重复表格列。以下是截图:

首先我搜索 Dosa:

然后我关闭那个 Frame 并搜索 Idli,看看它是怎么来的:

谁能帮助我并告诉我哪里出错了?这是我的数据库活动和检索代码。

search.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent ae){
                try {
                     s=field.getText();
                     Connection con = null;
                     Class.forName("com.mysql.jdbc.Driver");
                     con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/delikat", 
                            "root", "");
                     Statement st = (Statement) con.createStatement();
                     ResultSet rs= (ResultSet) st.executeQuery( "SELECT * FROM delicious where name = '"+s+ "'" );
                     ResultSetMetaData md = (ResultSetMetaData) rs.getMetaData();
                     int columns = md.getColumnCount();
                     for (int i = 1; i <= columns; i++) {
                        columnNames.addElement( md.getColumnName(i) );
                        }
                     while (rs.next()) {
                         Vector<Object> row = new Vector<Object>(columns);
                         for (int i = 1; i <= columns; i++) {
                             row.addElement( rs.getObject(i) );
                         }
                         data.addElement( row );
                     }
                     rs.close();
                     st.close();                         
                }
                catch(Exception e) {
                    e.printStackTrace();
                }
                Vector<Vector<Object>> NULL = null;
                if(data==NULL) {
                        JOptionPane.showMessageDialog(frame, "No Data Found");
                }
                    else {
                //Declare a new Frame for the Query Result Display
                JFrame tab=new JFrame();

                //Add the Data and the Column Names to the Table
                JTable table = new JTable(data, columnNames);

                //Add Scroll Pane for instances when more data is to be displayed
                JScrollPane scrollPane = new JScrollPane( table );

                /* Open the Result of the Query in a new Frame, in a Tabular Format with Scroll Pane.
                   Add all the elements to the Frame and set the specifications of the Frame like
                   Size, Visibility, etc.
                */
                tab.add( scrollPane );
                tab.setVisible(true);
                tab.setSize(810,100);
                }
                }
            });

请帮我解决这个问题。谢谢。

【问题讨论】:

    标签: java swing jdbc applet jtable


    【解决方案1】:
    • 如果Vector&lt;Vector&lt;Object&gt;&gt; data = new Vector&lt;Vector&lt;Object&gt;&gt;(伪代码)被重用,则需要重新初始化data = new Vector&lt;Vector&lt;Object&gt;&gt;,那么旧的二维数组就是一个没有旧元素的新数组

    • 使用XxxTableModel for underlaying data instead 重新创建整个数组,可以作为标准的可能方式

    【讨论】:

    • 我修好了,还是谢谢你。另一种方法也有效。如果在表中找不到要搜索的对象,您能否帮我显示一个错误。我已经更新了我尝试过的代码,但它不起作用。
    • zhins 像你一样简单......,如果执行 while (rs.next()) { 内部的代码行,ResultSet 不为空,但不超过 2k行将这些数据加载到内存中,在 JTable 中使用标准过滤(请参阅我的答案中的链接)或 fpr 详细信息在此处搜索 jtable + rowsorter(变体排序、tablerowsorter 等),
    【解决方案2】:

    试试:

    data.clear();
    

    之前

      while (rs.next()) {...
    

    【讨论】:

    • 是的,它清除了之前的数据,但是列名水平出现了两次。
    • 是的,添加了 columnNames.clear();在for循环之前。万分感谢。 :)
    • 知道如果找不到要搜索的内容如何显示错误吗?一种自定义消息。
    • 不止一种方式 - 请参阅this one
    • 我如何在这里实现一个状态的东西,它是如何相关的?你能解释一下吗?我尝试检查数据是否为空,如果为空则弹出一个弹出窗口,否则显示表格。似乎没有工作。你能告诉条件来检查我正在搜索的数据是否为空吗?
    猜你喜欢
    • 2016-03-30
    • 1970-01-01
    • 2016-11-08
    • 2018-05-22
    • 1970-01-01
    • 1970-01-01
    • 2018-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多