2016-10-04 80 views
0

我有一些問題,試圖從讀取器中檢測到另一個RFID標籤時,爲什麼將值從Tableview覆蓋到同一錶行和列。Tableview java顯示下一行數據庫

我想要的只是確保每個數據都能夠顯示下一個可用的行,看起來我無法解決此問題。我希望你們能幫忙。由於

這裏是我的表聲明

public class tableview extends Application implements Initializable { 

    @FXML 
    private TableView<UserDetails> table; 
    @FXML 
    private TableColumn<UserDetails, String> epc; 
    @FXML 
    private TableColumn<UserDetails, String> modCode; 
    @FXML 
    private TableColumn<UserDetails, String> modClass; 
    @FXML 
    private TableColumn<UserDetails, String> modName; 
    @FXML 
    private TableColumn<UserDetails, String> timestamp; 




    public void initialize(URL location, ResourceBundle resources) { 

     epc.setCellValueFactory(new PropertyValueFactory<>("epcNo")); 
     modCode.setCellValueFactory(new PropertyValueFactory<>("moduleCode")); 
     modClass.setCellValueFactory(new PropertyValueFactory<>("moduleClass")); 
     modName.setCellValueFactory(new PropertyValueFactory<>("moduleName")); 
     timestamp.setCellValueFactory(new PropertyValueFactory<>("timestamp")); 

public synchronized void moduleInfo(String epcString) { 
     try { 
      conn = db.getConnection(); 
      int i = 0; 
      ResultSet rs = conn.createStatement() 
        .executeQuery("SELECT * FROM module_info_table where rfid_tag_id = " + epcString); 

      while (rs.next()) { 
       String modulecode = rs.getString("module1_code"); 
       String moduleclass = rs.getString("module_class"); 
       String modulename = rs.getString("module1_name"); 
       String epc = rs.getString("rfid_tag_id"); 
       String timestamp = rs.getString("last_updated"); 

       System.out.println(modulecode); 
       if (epcString.equals(epc)) { 
        data2 = FXCollections.observableArrayList(); 
        data2.add(new UserDetails(epc, modulecode, moduleclass, modulename, timestamp)); 
       } 
      } 
      table.setItems(data2); 
      table.refresh(); 

      db.closeConn(); 
     } catch (Exception e) { 
      System.out.println(e); 

     } 

    } 
public class PrintListener implements ReadListener { 
    @Override 
    public void tagRead(Reader r, TagReadData tr) { 

     try { 

      String epcString = tr.getTag().epcString(); 

      if (!map.containsKey(epcString)) { 


       moduleInfo(epcString); < -- call to here 


      } 
      table.setItems(data); 
      table.refresh(); 

     } catch (Exception e) { 
      System.out.println(e); 
     } 
    } 

} 

這是我的tableview:

RFID TAG:45

EPC  Module Code Module Class  Module Name   TimeStamp 
    45  ET123  DCPE/FT/5D  Computer System  Mon Oct 4 01:13:23am 
    -   -    -     -     - 
    -   -    -     -     - 

新的RFID標籤:100(這將覆蓋以前的記錄)

EPC  Module Code Module Class  Module Name   TimeStamp 
    100  ET468  DEEE/FT/8G  Computer Science  Mon Oct 4 01:13:23am 
    -   -    -     -     - 
    -   -    -     -     - 

正如你可以看到下面的圖片: 第一個RFID標籤檢測,一旦它與MySQL數據庫

被檢測到新的RFID標籤後,與它匹配將顯示的內容,最新的RFID標籤覆蓋舊的記錄,這我不希望它發生。

請注意,tableview有很多列和行,因爲我不得不裁剪掉大量的空間。

回答

0
public void initialize(URL location, ResourceBundle resources) { 

    data2 = FXCollections.observableArrayList(); 

     epc.setCellValueFactory(new PropertyValueFactory<>("epcNo")); 
     modCode.setCellValueFactory(new PropertyValueFactory<>("moduleCode")); 
     modClass.setCellValueFactory(new PropertyValueFactory<>("moduleClass")); 
     modName.setCellValueFactory(new PropertyValueFactory<>("moduleName")); 
     timestamp.setCellValueFactory(new PropertyValueFactory<>("timestamp")); 

我我已經測試了將observablelist放在這裏,它工作! ((rs.next))循環

0

您在這裏覆蓋數據2:

data2 = FXCollections.observableArrayList(); 

刪除它,然後把它放在這裏

public synchronized void moduleInfo(String epcString) { 
    try { 
     data2 = FXCollections.observableArrayList(); 
     conn = db.getConnection(); 
     int i = 0; 
     ResultSet rs = conn.createStatement() 
       .executeQuery("SELECT * FROM module_info_table where rfid_tag_id = " + epcString); 

     while (rs.next()) { 
      String modulecode = rs.getString("module1_code"); 
      String moduleclass = rs.getString("module_class"); 
      String modulename = rs.getString("module1_name"); 
      String epc = rs.getString("rfid_tag_id"); 
      String timestamp = rs.getString("last_updated"); 

      System.out.println(modulecode); 
      if (epcString.equals(epc)) { 

       data2.add(new UserDetails(epc, modulecode, moduleclass, modulename, timestamp)); 
      } 
     } 
     table.setItems(data2); 
     table.refresh(); 

     db.closeConn(); 
    } catch (Exception e) { 
     System.out.println(e); 

    } 

現在應該如果我不是盲目地fixxed:P

+0

它仍然給了我相同的結果\ – Atrix

+0

感謝提示我終於解決了問題 – Atrix

+0

幹得好;)甚至如果我沒有修復它 –