简体   繁体   中英

Refresh JTable after executing SQL UPDATE statement

I created a JTable and defined MyTableModel in order to populate this table from MySQL DB. However, the problem is that when I execute UPDATE statement, then an additional row is included in JTable (but not in MySQL DB).

How should I update my code to be able to properly update JTable without re-opening the application? (I'm using fireTableChanged(null))

 public class QueryTableModel extends AbstractTableModel {
      Vector cache;
      int colCount;
      String[] headers;

      String db;
      String dbHost;
      String dbLogin;
      String dbPassw;     
      Statement statement;

      public QueryTableModel() {
        cache = new Vector();
      }

      public String getColumnName(int i) {
        return headers[i];
      }

      public int getColumnCount() {
        return colCount;
      }

      public int getRowCount() {
        return cache.size();
      }

      public Object getValueAt(int row, int col) {
        return ((String[]) cache.elementAt(row))[col];
      }

      public Connection getConnection()
                throws Exception {
            String dbURL = "";
            try {
                Class.forName("com.mysql.jdbc.Driver");
            } catch (ClassNotFoundException e) {
                System.out.println("Cannot find the MySQL JDBC driver. ");
                throw (e);
            }
            try {
                dbURL = "jdbc:mysql://" + dbHost + "/" + db;
                // WINDOWS
                Connection con = DriverManager.getConnection(dbURL,dbLogin,dbPassw);
                // LINUX
                // Connection con = DriverManager.getConnection(url);
                return con;
            } catch (SQLException e) {
                System.out.println("No connection with " + dbURL);
                throw (e);
            }
        }

      public void setQuery(String query) {
        cache = new Vector();
        try {
          // Execute the query and store the result set and its metadata
          Connection con = getConnection();
          Statement statement = con.createStatement();
          ResultSet rs = statement.executeQuery(query);
          ResultSetMetaData meta = rs.getMetaData();
          colCount = meta.getColumnCount();

          // Rebuild the headers array with the new column names
          headers = new String[colCount];
          for (int h = 1; h <= colCount; h++) {
            headers[h - 1] = meta.getColumnName(h);
          }

          while (rs.next()) {
            String[] record = new String[colCount];
            for (int i = 0; i < colCount; i++) {
              record[i] = rs.getString(i + 1);
            }
            cache.addElement(record);
          }
          fireTableChanged(null);

          rs.close();
          if (con.getAutoCommit() != false) {
            con.close();
          }

        } catch (Exception e) {
          cache = new Vector(); // blank it out and keep going.
          e.printStackTrace();
        }
      }

      public void setQueryUpdate(String[] data, String q) {
        try {
          Connection con = getConnection();
          Statement statement = con.createStatement();
          int rs = statement.executeUpdate(q);
          String[] record = new String[data.length];
          for (int i = 0; i < data.length; i++) {
            record[i] = data[i];
          }
          cache.addElement(record);
          fireTableChanged(null);
          if (con.getAutoCommit() != false) {
            con.close();
          }
        } catch (Exception e) {
          cache = new Vector(); // blank it out and keep going.
          System.out.println(e.getMessage());
        }
      }

    }

Now I see what you're trying here. You need to call:

fireTableDataChanged();

If you call fireTableChange(null) all the listeners will be called with Event equal to null. Depending on the implementation it will a) throw a NPE or b) do nothing. The fireTableDataChanged(); tells listeners that data has changed and to update accordingly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM