执行SQL UPDATE语句后刷新JTable

时间:2011-12-28 15:40:57

标签: java mysql swing refresh

我创建了一个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());
        }
      }

    }

1 个答案:

答案 0 :(得分:1)

现在我看到你在这里尝试的是什么。你需要打电话:

fireTableDataChanged();

如果你调用fireTableChange(null),将调用Event等于null的所有侦听器。根据实施情况,它将a)抛出NPE或b)什么也不做。 fireTableDataChanged();告诉侦听器数据已更改并相应更新。