title: How to update data in JTable? (with code)
body: <p>I have a JTable that I created with the Eclipse and populated the table by using Vector objects. I want the database (that gives the data to my jtable) to be updated after a change is made to the jtable. I don't really know how can i do that. Some suggestions?
  I use this code to populate the JTable:</p>
  
  <pre><code>import java.awt.BorderLayout;
  import java.awt.EventQueue;
  import java.awt.HeadlessException;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.ResultSetMetaData;
  import java.sql.Statement;
  import java.util.Vector;
  import java.util.logging.Level;
  import javax.swing.JButton;
  import javax.swing.JFrame;
  import javax.swing.JScrollPane;
  import javax.swing.JTable;
  import javax.swing.SwingWorker;
  import javax.swing.WindowConstants;
  import javax.swing.table.DefaultTableModel;
  
  public class provemain2 {
  
  
  
   public static void main(String[] args) {
          EventQueue.invokeLater(new Runnable() {
              @Override
              public void run() {
                  new provemain2();
              }
          });
      }
  
      private final JButton button;
      private final JTable table;
      private final DefaultTableModel tableModel = new DefaultTableModel();
  
      public provemain2() throws HeadlessException {
  
          JFrame one =new JFrame();
  
          one.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
          one.setVisible(true);
  
          table = new JTable(tableModel);
          one.add(new JScrollPane(table), BorderLayout.CENTER);
  
          button = new JButton("Load Data");
          button.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                  new SwingWorker&lt;Void, Void&gt;() {
                      @Override
                      protected Void doInBackground() throws Exception {
                          loadData();
                          return null;
                      }
                  }.execute();
              }
          });
          one.add(button, BorderLayout.PAGE_START);
  
          one.setSize(640, 480);
      }
  
      private void loadData() {
          //LOG.info("START loadData method");
  
          button.setEnabled(false);
  
          try (Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root","psw");
                  Statement stmt = conn.createStatement()) {
  
              ResultSet rs = stmt.executeQuery("select * from Acquirenti");
              ResultSetMetaData metaData = rs.getMetaData();
  
              // Names of columns
              Vector&lt;String&gt; columnNames = new Vector&lt;String&gt;();
              int columnCount = metaData.getColumnCount();
              for (int i = 1; i &lt;= columnCount; i++) {
                  columnNames.add(metaData.getColumnName(i));
              }
  
              // 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 i = 1; i &lt;= columnCount; i++) {
                      vector.add(rs.getObject(i));
                  }
                  data.add(vector);
              }
  
              tableModel.setDataVector(data, columnNames);
          } catch (Exception e) {
              //LOG.log(Level.SEVERE, "Exception in Load Data", e);
          }
          button.setEnabled(true);
  
          //LOG.info("END loadData method");
      }
  
  }
  </code></pre>