簡體   English   中英

在執行SQL UPDATE語句后刷新JTable

[英]Refresh JTable after executing SQL UPDATE statement

我創建了一個JTable並定義了MyTableModel以便從MySQL DB填充此表。 但是,問題在於,當我執行UPDATE語句時,JTable中將包含另一行(但MySQL DB中沒有)。

我應該如何更新代碼以能夠正確更新JTable而無需重新打開應用程序? (我正在使用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());
        }
      }

    }

現在,我看到您在這里嘗試什么。 您需要致電:

fireTableDataChanged();

如果調用fireTableChange(null) ,則將以Event等於null的方式調用所有偵聽器。 取決於實現方式,它將a)拋出NPE或b)不執行任何操作。 fireTableDataChanged(); 告訴偵聽器數據已更改並相應更新。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM