title: Display data from mysql in JTable
body: <p>How can I inster my data from mysql table into my JTable? And I would like to be able to make UPDATE,DELETE and ADD operation into mysql table.
  This should my a restaurant application and I want the user to see in that JTable the restaurant stock and clicking the table button it opens a new frame where the user can add or delete or cash in products from the stock to that table. It would be cool that mysql table to auto update once a product is added or deleted from the table consumation.</p>
  
  <pre><code>package project_bar;
  
  
  import java.sql.Connection;
  import java.sql.PreparedStatement;
  import java.sql.ResultSet;
  import javax.swing.JOptionPane;
  import java.awt.*;
  import java.sql.*;
  import java.util.*;
  import javax.swing.*;
  import javax.swing.table.*;
  
  
  public class MainMenu extends javax.swing.JFrame {
  Connection conn = new DBConnection().connect();
  PreparedStatement pst = null;
  ResultSet rs = null;
      public MainMenu() {
          initComponents();
  
      }
  
  
      @SuppressWarnings("unchecked")
      // &lt;editor-fold defaultstate="collapsed" desc="Generated Code"&gt;                          
      private void initComponents() {
  
          jPanel1 = new javax.swing.JPanel();
          logout_bt = new javax.swing.JButton();
          jLabel1 = new javax.swing.JLabel();
          table1_bt = new javax.swing.JButton();
          table1_lb = new javax.swing.JLabel();
          table2_lb = new javax.swing.JLabel();
          table2_bt = new javax.swing.JButton();
          table3_lb = new javax.swing.JLabel();
          table3_bt = new javax.swing.JButton();
          Bauturi_lb = new javax.swing.JLabel();
          jScrollPane1 = new javax.swing.JScrollPane();
          Table_stock = new javax.swing.JTable();
  
          setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
  
          logout_bt.setText("LOGOUT");
          logout_bt.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  logout_btActionPerformed(evt);
              }
          });
  
          jLabel1.setFont(new java.awt.Font("Verdana", 3, 18)); // NOI18N
          jLabel1.setText("Tamas Vlad Restaurant");
  
          table1_bt.setForeground(new java.awt.Color(204, 204, 204));
          table1_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/round_table-128.png"))); // NOI18N
          table1_bt.setText("TABLE 1");
          table1_bt.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  table1_btActionPerformed(evt);
              }
          });
  
          table1_lb.setText("TABLE 1");
  
          table2_lb.setText("Table 2");
  
          table2_bt.setForeground(new java.awt.Color(204, 204, 204));
          table2_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/round_table-128.png"))); // NOI18N
          table2_bt.setText("TABLE 1");
          table2_bt.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  table2_btActionPerformed(evt);
              }
          });
  
          table3_lb.setText("Table 3");
  
          table3_bt.setForeground(new java.awt.Color(204, 204, 204));
          table3_bt.setIcon(new javax.swing.ImageIcon(getClass().getResource("/round_table-128.png"))); // NOI18N
          table3_bt.setText("TABLE 1");
          table3_bt.addActionListener(new java.awt.event.ActionListener() {
              public void actionPerformed(java.awt.event.ActionEvent evt) {
                  table3_btActionPerformed(evt);
              }
          });
  
          Bauturi_lb.setFont(new java.awt.Font("Vani", 3, 24)); // NOI18N
          Bauturi_lb.setText("DRINKS ON STOCK");
  
          Table_stock.setModel(new javax.swing.table.DefaultTableModel(
              new Object [][] {
                  {null, null, null},
                  {null, null, null},
                  {null, null, null},
                  {null, null, null}
              },
              new String [] {
                  "Name", "Quantity", "Price"
              }
          ) {
              boolean[] canEdit = new boolean [] {
                  false, false, false
              };
  
              public boolean isCellEditable(int rowIndex, int columnIndex) {
                  return canEdit [columnIndex];
              }
          });
          Table_stock.setCellSelectionEnabled(true);
          Table_stock.addAncestorListener(new javax.swing.event.AncestorListener() {
              public void ancestorMoved(javax.swing.event.AncestorEvent evt) {
              }
              public void ancestorAdded(javax.swing.event.AncestorEvent evt) {
                  Table_stockAncestorAdded(evt);
              }
              public void ancestorRemoved(javax.swing.event.AncestorEvent evt) {
              }
          });
          jScrollPane1.setViewportView(Table_stock);
          Table_stock.getColumnModel().getSelectionModel().setSelectionMode(javax.swing.ListSelectionModel.SINGLE_INTERVAL_SELECTION);
  
          javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
          jPanel1.setLayout(jPanel1Layout);
          jPanel1Layout.setHorizontalGroup(
              jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(jPanel1Layout.createSequentialGroup()
                  .addContainerGap()
                  .addComponent(logout_bt)
                  .addGap(334, 334, 334)
                  .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 243, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addGap(0, 599, Short.MAX_VALUE))
              .addGroup(jPanel1Layout.createSequentialGroup()
                  .addGap(41, 41, 41)
                  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                      .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                      .addComponent(Bauturi_lb, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE))
                  .addGap(138, 138, 138)
                  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                      .addComponent(table1_bt, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addComponent(table1_lb, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addGap(123, 123, 123)
                  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                      .addComponent(table2_bt, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE)
                      .addComponent(table2_lb, javax.swing.GroupLayout.PREFERRED_SIZE, 47, javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addGap(135, 135, 135)
                  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                      .addComponent(table3_lb)
                      .addComponent(table3_bt, javax.swing.GroupLayout.PREFERRED_SIZE, 84, javax.swing.GroupLayout.PREFERRED_SIZE))
                  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
          );
          jPanel1Layout.setVerticalGroup(
              jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(jPanel1Layout.createSequentialGroup()
                  .addContainerGap()
                  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                      .addComponent(jLabel1)
                      .addComponent(logout_bt))
                  .addGap(68, 68, 68)
                  .addComponent(Bauturi_lb, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                      .addGroup(jPanel1Layout.createSequentialGroup()
                          .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 124, Short.MAX_VALUE)
                          .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                              .addComponent(table1_lb)
                              .addComponent(table2_lb)
                              .addComponent(table3_lb))
                          .addGap(24, 24, 24)
                          .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                              .addComponent(table1_bt, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                              .addComponent(table2_bt, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                              .addComponent(table3_bt, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)))
                      .addGroup(jPanel1Layout.createSequentialGroup()
                          .addGap(38, 38, 38)
                          .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)))
                  .addGap(556, 556, 556))
          );
  
          javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
          getContentPane().setLayout(layout);
          layout.setHorizontalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(layout.createSequentialGroup()
                  .addContainerGap()
                  .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addContainerGap(19, Short.MAX_VALUE))
          );
          layout.setVerticalGroup(
              layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
              .addGroup(layout.createSequentialGroup()
                  .addContainerGap()
                  .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                  .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
          );
  
          pack();
      }// &lt;/editor-fold&gt;                        
  
      private void table1_btActionPerformed(java.awt.event.ActionEvent evt) {                                          
  
          setDefaultCloseOperation(DISPOSE_ON_CLOSE);
           new Table1().show();
  
      }                                         
  
      private void logout_btActionPerformed(java.awt.event.ActionEvent evt) {                                          
          this.dispose();
               new Login(null,true).show();
  
      }                                         
  
      private void table2_btActionPerformed(java.awt.event.ActionEvent evt) {                                          
          // TODO add your handling code here:
          setDefaultCloseOperation(DISPOSE_ON_CLOSE);
           new Table1().show();
      }                                         
  
      private void table3_btActionPerformed(java.awt.event.ActionEvent evt) {                                          
          // TODO add your handling code here:
          setDefaultCloseOperation(DISPOSE_ON_CLOSE);
           new Table1().show();
  
      }                                         
  
      private void Table_stockAncestorAdded(javax.swing.event.AncestorEvent evt) {                                          
          // TODO add your handling code here:
  
      }                                         
  
  
  
      public static void main(String args[]) {
  
          java.awt.EventQueue.invokeLater(new Runnable() {
              public void run() {
                  new MainMenu().setVisible(true);
              }
          });
      }
  
      // Variables declaration - do not modify                     
      private javax.swing.JLabel Bauturi_lb;
      private javax.swing.JTable Table_stock;
      private javax.swing.JLabel jLabel1;
      private javax.swing.JPanel jPanel1;
      private javax.swing.JScrollPane jScrollPane1;
      private javax.swing.JButton logout_bt;
      private javax.swing.JButton table1_bt;
      private javax.swing.JLabel table1_lb;
      private javax.swing.JButton table2_bt;
      private javax.swing.JLabel table2_lb;
      private javax.swing.JButton table3_bt;
      private javax.swing.JLabel table3_lb;
      // End of variables declaration                   
  }
  </code></pre>