title: JAVA update database on JTable cell change
body: <p>He I am noob to Java (Thats not new) and I can't find a good tutorial for this. I use jTable to display a table filled with data from a MySQL database. </p>
  
  <p>So far so good, I get the table:</p>
  
  <p><img src="https://i.stack.imgur.com/bwPQa.png" alt="table preview"></p>
  
  <p>Standard you can click a table cell and it changes to a text-field with can be filled with something new. You all know that, but how can I use this to also update the value in my database?</p>
  
  <p>My code:</p>
  
  <pre><code>import [...];
  public class table extends JPanel {
      public String table;
      private Database db;
  
      public table(String tablename, Database db){
  
          try {
              table = tablename;
              this.db = db;
  
              //Get table with and height
              ResultSet res = db.query("SELECT COUNT( * ) FROM `"+table+"`");
              ResultSet res2 = db.query("SELECT COUNT( * ) FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = '"+table+"'");
  
              int rows = 0;
              int collums = 0;
  
              res.next();
              res2.next();
  
              rows = res.getInt(1);
              collums = res2.getInt(1);
  
              //Get table column names and set then in array
              ResultSet clom = db.query("DESCRIBE  `"+table+"`");
  
              String[] columnNames = new String[collums];
  
              int s = 0;
  
              while(clom.next()){
                  columnNames[s] = clom.getString(1);
                  s++;
              }
  
              //get table data and put in array
              Object[][] data = new Object[rows][collums];
  
              ResultSet result = db.query("SELECT * FROM `"+table+"`");
  
              int q = 0;
  
              while(result.next()){
                  for(int a=0; a&lt;= (collums - 1); a++){
                      data[q][a] = result.getString(a + 1);
                      //System.out.println(q + " - " + a);
                  }
                  q++;
              }
  
              //Make Jtable of the db result form the two array's
              final JTable table = new JTable(data, columnNames);
              table.setPreferredScrollableViewportSize(new Dimension(500, 70));
              table.setFillsViewportHeight(true);
  
              // do some event listening for cell change
  
              JScrollPane scrollPane = new JScrollPane(table);
              JFrame frame = new JFrame("table editor");
              scrollPane.setOpaque(true);
              frame.setContentPane(scrollPane);
              frame.pack();
              frame.setSize(600, 800);
              frame.setVisible(true);
          } catch (SQLException e1) {
              e1.printStackTrace();
          }
      }
  }
  </code></pre>
  
  <p>I guess I need to bind some kind of table listener and when something changes I take all the values of the table and update them with a query. </p>
  
  <p>How can I do this?</p>
  
  <p>This is my pseudo code:</p>
  
  <pre><code>table.bindCellEventListner(callback(t){
       Array row = t.getAllValuesAsArrayOfRow();
  
       String data = "";
  
       int f = 0
       while(row.next()){
           data .= "`"+clom[f]+"` = '"+row[f]+"',"
           f++;
       }
       data.delLastChar();
       db.query("UPDATE `"+table+"` SET "+data+" WHERE `id` ="+row[0]+";");
  
  });
  </code></pre>