Ankur Sinha
Ankur Sinha

Reputation: 6639

Not able to clear the previous table data

I have made an applet with a search engine functionality. The problem here is, when I am trying to search using a keyword first, it is displayed properly by opening a new JFrame in JTable format. But when I close that frame and enter something else in the search form to search something else, it not only shows the latest thing I have searched for but also those things I searched for before. And horizontally, it keeps repeating the table columns too. Here are screenshots:

First I search for Dosa: Image 1

And then I close that Frame and search for Idli, look how it comes: Image 2

Can anyone help me and tell me where I am going wrong? Here is my code for the Database activity and retrieval thing.

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);
                }
                }
            });

Please help me overcome this problem. Thank You.

Upvotes: 0

Views: 350

Answers (2)

mKorbel
mKorbel

Reputation: 109813

  • required to reinitialize data = new Vector<Vector<Object>> if Vector<Vector<Object>> data = new Vector<Vector<Object>> (pseudo code) is reused then old 2D array is a new without old elements

  • use XxxTableModel for underlaying data instead of recreate a whole array, could be as standard of possible ways

Upvotes: 2

Fergus
Fergus

Reputation: 220

Try:

data.clear();

before

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

Upvotes: 1

Related Questions