title: Refresh Database Connection
body: <p>I have a Java application that connects to a MySQL database and uses a select statement to display rows whose status is "O" through a JTable. I have added a mouse event so that once the selected row is clicked, the details of the row are displayed in another GUI form. After a mouse event on the new form, the database table is updated to status "R". When I go back to the original display on the JTable, I expect the row to no longer be displayed. However, I find the row I had selected earlier still displayed on the JTable and I have to exit the entire program for the JTable to refresh with the updated status.</p>
  
  <p>The table status is getting updated to "R" and am using</p>
  
  <pre><code>setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  </code></pre>
  
  <p>to navigate from each form. Is there a way to refresh the database connection so that once I go back to the display GUI, the table updates using the changed status?</p>
  
  <p>I display using this code:</p>
  
  <pre><code>public class ReceiveOrderedParts extends JFrame{
  static Vector&lt;Vector&lt;String&gt;&gt; data = new Vector&lt;Vector&lt;String&gt;&gt;();
  static JTable table;
  ReceiveOrderedParts(final String st) throws InterruptedException, SQLException{             
      super("Order Details Inquiry");
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
      JPanel topPanel = new JPanel();
  
      getContentPane().add(topPanel, BorderLayout.PAGE_START);
  
      Vector&lt;String&gt; headers = new Vector&lt;String&gt;();
      headers.add("Order Number");
      headers.add("Date");       
      headers.add("Part Number");
      headers.add("Part Name");
      headers.add("Application");
      headers.add("Quantity");  
      headers.add("Total Cost");
      headers.add("Supplier");                       
      headers.add("Country");
  
      getData();
  
      //this is the model which contain actual body of JTable
      DefaultTableModel model = new DefaultTableModel(data, headers);
      table = new JTable(model);
      table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);       
      //table.enable(false);
      table.addMouseListener(new ReceiveOrderedParts.MyMouseAdapter());
      boolean b = table.isCellEditable(WIDTH, WIDTH);
      b = false;
  
      header_size();
  
      JScrollPane scroll = new JScrollPane(table);
  
      scroll.setHorizontalScrollBarPolicy(
              JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
      scroll.setVerticalScrollBarPolicy(
              JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
  
      getContentPane().add(scroll, BorderLayout.PAGE_START);
  
      pack();
      setResizable(false);
      setVisible(true);
      setSize(1000, 700);
  
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  }
  public class MyMouseAdapter extends MouseAdapter  {
  
      public void mousePressed(MouseEvent e)  {         
              int colIdx = table.columnAtPoint(e.getPoint());                
              int rowIdx = table.rowAtPoint(e.getPoint());
             Object obj = table.getModel().getValueAt(rowIdx, colIdx) ;
                String order = obj.toString();                
              JOptionPane.showMessageDialog(null,"Receive Order "+ order + "?", "Success",
                          JOptionPane.INFORMATION_MESSAGE);
                     // return;
          try {
              SelectOrder seo = new SelectOrder(order);
              seo.setVisible(true);
              setVisible(false);
          } catch (InterruptedException ex) {
              Logger.getLogger(ReceiveOrderedParts.class.getName()).log(Level.SEVERE, null, ex);
          }
  
      }
  
  }
  
  /**
   * Setting the particular Column Size in JTable
   */
  public static void header_size() {
      TableColumn column = table.getColumnModel().getColumn(0);
      column.setPreferredWidth(100);
  
      column = table.getColumnModel().getColumn(1);
      column.setPreferredWidth(100);
  }
  
  /**
   * Fetching Data From MySql Database and storing in a Vector of a Vector to
   * Display in JTable
   */
  private static void getData() throws SQLException {
      // Enter Your MySQL Database Table name in below Select Query.
      String str = "select order_number, Part_number, spare_desc, vehicle_app, qnty_ordered,"
              + "seller_name, total_purchases, country, order_date from order_details_table where status = 'O'";
      Connection cn = null;
      ResultSet rs;
      Statement st;
  
      try {
          // Change the database name, hosty name, 
          // port and password as per MySQL installed in your PC.
          cn = DriverManager.getConnection("jdbc:mysql://"
                  + "localhost:3306/test", "root", "gatungo@123");
          st = cn.createStatement();
  
          rs = st.executeQuery(str);
  
          while (rs.next()) {
              Vector&lt;String&gt; d = new Vector&lt;String&gt;();
  
              d.add(rs.getString("order_number"));
              d.add(rs.getString("order_date"));                
              d.add(rs.getString("Part_number"));
              d.add(rs.getString("spare_desc"));
              d.add(rs.getString("vehicle_app"));
              d.add(rs.getString("qnty_ordered"));  
              d.add(rs.getString("total_purchases"));
              d.add(rs.getString("seller_name"));                                                
              d.add(rs.getString("country"));
  
              d.add("\n\n\n\n\n\n\n");
              data.add(d);
  
          }
          cn.close();
  
      } catch (SQLException e) {
  
          e.printStackTrace();
  
      }
  }
  }
  </code></pre>
  
  <p>I go back with this code;</p>
  
  <pre><code>Connection conn = null;
  String url = "jdbc:mysql://localhost:3306/";
  String db = "test";
  String driver = "com.mysql.jdbc.Driver";
  Class.forName(driver);
  conn = DriverManager.getConnection(url + db, "user", "password");
  Statement st = conn.createStatement();
  String partno = tpart.getText();
  String sparedesc = tdesc.getText();
  String vapp = tapp.getText();
  String qunty = tqnty.getText();
  String costper = tcost.getText();
  String country = tcountry.getText();
  String retail = tretail.getText();
  String store = tstore.getText();
  String location = tlocation.getText();
  String rdate = trdate.getText();
  String storeit = "INSERT INTO Store_info_table (Part_number, spare_desc, vehicle_app, qnty_received, cost_per_unit"
          + ", selling_price, country, store_name, store_location, recvd_date) "
          + "VALUES ('" + partno + "', '" + sparedesc + "', '" + vapp + "', '" + qunty + "', '" + costper + "','" + retail + "','" + country + "','" + store + "','" + location + "','" + rdate + "' )";
  st.execute(storeit);
  
  String updateOrder = "UPDATE order_details_table set status = 'R' where order_number = '" + order + "'";
  st.execute(updateOrder);
  
  JOptionPane.showMessageDialog(null,"Item Stored Successfully", "Success",
          JOptionPane.INFORMATION_MESSAGE);
  conn.close();
  
  ReceiveOrderedParts rop = new ReceiveOrderedParts(order);
  rop.invalidate();
  rop.setVisible(true);
  setVisible(false);                        
  </code></pre>