2016-05-03 66 views
0

我想從數據庫中獲取數據並在jtree中顯示。我面臨麻煩的ResultSet顯示下面我的代碼如何在jtree中顯示數據

rs1=DB.Select("SELECT AcCode from tblCoa order by AcCode"); 

while(rs1.next()){ 
    DefaultMutableTreeNode Catagory=new DefaultMutableTreeNode(rs1.getString("AcCode")); 
    DefaultMutableTreeNode Catagories=new DefaultMutableTreeNode(rs1.getString("AcCode")); 
    DefaultMutableTreeNode Catagories2=new DefaultMutableTreeNode(rs1.getString("AcCode")); 
    DefaultMutableTreeNode Catagories3=new DefaultMutableTreeNode(rs1.getString("AcCode")); 
    DefaultMutableTreeNode Catagories4=new DefaultMutableTreeNode(rs1.getString("AcCode")); 
    Catagory.add(Catagories); 
    Catagories.add(Catagories2); 
    Catagories.add(Catagories3); 
    Catagories.add(Catagories4); 
    DefaultTreeModel dtm=new DefaultTreeModel(Catagory); 
    this.jTree1.setModel(dtm); 
} 
+1

你可以把所有的代碼放在一個代碼塊中,並描述你的輸出有什麼問題嗎? – mabe02

+0

private void FillData()引發SQLException ResultSet rs1 = null; rs1 = DB.Select(「通過AcCode從tblCoa命令中選擇AcCode」); (rs1.next()){DefaultMutableTreeNode Catagory = new DefaultMutableTreeNode(rs1.getString(「AcCode」)); DefaultMutableTreeNode Catagories = new DefaultMutableTreeNode(rs1.getString(「AcCode」)); –

+0

DefaultMutableTreeNode Catagories2 = new DefaultMutableTreeNode(rs1.getString(「AcCode」)); DefaultMutableTreeNode Catagories3 = new DefaultMutableTreeNode(rs1.getString(「AcCode」)); DefaultMutableTreeNode Catagories4 = new DefaultMutableTreeNode(rs1.getString(「AcCode」)); Catagory.add(Catagories); Catagories.add(Catagories2); Catagories.add(Catagories3); Catagories.add(Catagories4); DefaultTreeModel dtm = newDefaultTreeModel(Catagory); this.jTree1.setModel(dtm);} –

回答

1
DefaultMutableTreeNode Catagory = new DefaultMutableTreeNode(); 


    while(rs1.next()){ 
     Catagory.add(rs1.getString("AcCode")); 
    } 
      DefaultTreeModel dtm=new DefaultTreeModel(Catagory); 
      this.jTree1.setModel(dtm); 

的最後一個值data`您啓動while循環中的變量。這將重置每個循環中的先前數據,並且您有多個DefaultMutableTreeNode變量,這些變量將存儲相同的rs1.getString("AcCode")。我沒有看到原因。對代碼進行了一些更改,現在就試用它。

+0

我面臨的問題是檢索整個結果集。我只是得到結果集中的最後一個值。 –

+0

現在做了一些改變。 – Priyamal

+0

我改變它,但我只是得到resultSet中的最後一個值不是整個resultSet –