0

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Ankur Sinha
  • 6,473
  • 7
  • 42
  • 73

2 Answers2

2
  • 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

mKorbel
  • 109,525
  • 20
  • 134
  • 319
  • I fixed it, thank you anyways. The alternative method worked too. Could you please help me display an error if the object to be searched for is not found in the table. I've updated the code of what I've tried but it's not working. – Ankur Sinha Apr 19 '13 at 17:36
  • zhins are simpler as you ..., ResultSet isn't empty in the case that code lines inside while (rs.next()) { are executed, but is isn't there more than 2k rows load those data to the memory, use standard FIltering in JTable (see link in my answer here) or fpr details search here jtable + rowsorter (variations sorting, tablerowsorter etc), – mKorbel Apr 19 '13 at 18:04
1

Try:

data.clear();

before

  while (rs.next()) {...
Fergus
  • 220
  • 1
  • 7
  • Yes, it cleared the previous data, but the column names appear twice horizontally. – Ankur Sinha Apr 19 '13 at 16:41
  • Yes, added columnNames.clear(); before the for-loop. Thanks a ton. :) – Ankur Sinha Apr 19 '13 at 16:44
  • Any idea how to display an error if something to be searched for is not found? A custom message sort of. – Ankur Sinha Apr 19 '13 at 16:44
  • More than one way - see [this one](http://stackoverflow.com/questions/3035880/how-can-i-create-a-bar-in-the-bottom-of-a-java-app-like-a-status-bar) – Fergus Apr 19 '13 at 16:47
  • How do I implement a status thing here and how is it related? Could you please explain. I tried checking for the data to be empty, if empty then throw a pop up else show the table. Doesn't seem to be working. Can you tell the condition to check if the data I am searching for is empty or not? – Ankur Sinha Apr 19 '13 at 16:55
  • What have you tried? You should catch "DataNotFoundException" and then display message - via StatusBar or JOptionPane - whichever you prefer. – Fergus Apr 19 '13 at 17:11
  • After the try catch statement, I put if(data==null) {joption thing} else {the new jframe, new table and all}; I put DataNotFoundException after the try statement. Eclipse gives errors. – Ankur Sinha Apr 19 '13 at 17:17
  • Check the updated code after try-catch. I am checking if the data is null, meaning nothing is there, then open a pop up, else open a new frame and show the details. But it opens a new frame, prints the column names always instead. – Ankur Sinha Apr 19 '13 at 17:32