title: Java: Refresh a JTable in a if-loop using PreparedStatement (sql)
body: <p>I'm trying to make a research window that would be able to find an information in a PostgreSql database using Phone number OR name of the person I look for. My problem is that my JTable is in a If-loop ( To check if the user search with the name or the phone number ) so if the user use the window to search two different people, my code will add a new JTable below the first one instead of refreshing it.</p>
  
  <p>Here is the ActionListener that cause my problem.</p>
  
  <pre><code>        class OkListener implements ActionListener {
          public void actionPerformed(ActionEvent arg0){
              try {
  
              String name = nameField.getText();
              String phone = phoneField.getText();
              Connection conn = CCConnection.getInstance();
              if (!phone.isEmpty() &amp;&amp; name.isEmpty() ){
  
  
                  String sqlName = "select * from creditcopie where telephone = ?";
                  PreparedStatement preparedStatementName = CCConnection.getInstance().prepareStatement(sqlName, ResultSet.TYPE_SCROLL_SENSITIVE, 
                          ResultSet.CONCUR_UPDATABLE);
                  preparedStatementName.setString(1, phone);
                  ResultSet rs = preparedStatementName.executeQuery();
                  JTable table = new JTable(buildTableModel(rs));
                  container.add(new JScrollPane(table));
                  container.revalidate();
                  container.repaint();
  
  
              }
  
              else if (phone.isEmpty() &amp;&amp; !name.isEmpty() ){
                  String sqlName = "select * from creditcopie where nom = ?";
                  PreparedStatement preparedStatementName = CCConnection.getInstance().prepareStatement(sqlName, ResultSet.TYPE_SCROLL_SENSITIVE, 
                          ResultSet.CONCUR_UPDATABLE);
                  preparedStatementName.setString(1, name);
                  ResultSet rs = preparedStatementName.executeQuery();
                  JTable table = new JTable(buildTableModel(rs));
                  container.add(new JScrollPane(table));
                  container.revalidate();
                  container.repaint();
                  confirmButton.setEnabled(false);
  
              }
  
              else{
                      JOptionPane.showMessageDialog(null, "Unable to do the research", "Error! ", JOptionPane.WARNING_MESSAGE);   
              }
  
  
  
              } catch (SQLException e) {
  
                  e.printStackTrace();
              }
  
  
      }
  </code></pre>
  
  <p>And this is the buildTableModel</p>
  
  <pre><code>            public DefaultTableModel buildTableModel(ResultSet rs)
                  throws SQLException {
  
              ResultSetMetaData metaData = rs.getMetaData();
  
              // names of columns
              Vector&lt;String&gt; columnNames = new Vector&lt;String&gt;();
              int columnCount = metaData.getColumnCount();
              for (int column = 1; column &lt;= columnCount; column++) {
                  columnNames.add(metaData.getColumnName(column));
              }
  
              // data of the table
              Vector&lt;Vector&lt;Object&gt;&gt; data = new Vector&lt;Vector&lt;Object&gt;&gt;();
              while (rs.next()) {
                  Vector&lt;Object&gt; vector = new Vector&lt;Object&gt;();
                  for (int columnIndex = 1; columnIndex &lt;= columnCount; columnIndex++) {
                      vector.add(rs.getObject(columnIndex));
                  }
                  data.add(vector);
              }
  
              return new DefaultTableModel(data, columnNames);
  
          }
  </code></pre>