2014-04-22 31 views
0

我打算將庫存記錄從數據庫顯示到jTable中。但是當我從數據庫檢索值到jTable時,它只顯示最後一個值。只有最後一個值是從jTable中的數據庫中打印

while(rs.next()) 
{ 
    String vendor = rs.getString("VENDOR"); 
    String p_type = rs.getString("P_TYPE"); 
    String p_name= rs.getString("P_NAME"); 
    String units = rs.getString("UNITS"); 
    String unit_price = rs.getString("UNIT_PRICE"); 

    Stock_Table.setValueAt(vendor, 1, 0); 
    Stock_Table.setValueAt(vendor, 2, 0); 
    Stock_Table.setValueAt(vendor, 3, 0); 
    Stock_Table.setValueAt(vendor, 4, 0); 
} 
+0

什麼是'Stock_Table'? – Mureinik

+0

爲了儘快提供更好的幫助,請發佈[MCVE](http://stackoverflow.com/help/mcve)(最小完整和可驗證示例)。 –

回答

4

首先,JavaDoc中清楚地表明,setValueAt(Object, int, int)該兩個int值表示的順序的行和列。

所以根據你的榜樣,要更新行1,2,3和4

其次的第一列,應避免使用setValueAt爲了這個目的,而是將行添加到TableModel

看看How to Use Tables瞭解更多詳情

1

試着給出如下代碼的增量行。

int currentRow=1; 
while(rs.next()) 
{ 
    String vendor = rs.getString("VENDOR"); 
    String p_type = rs.getString("P_TYPE"); 
    String p_name= rs.getString("P_NAME"); 
    String units = rs.getString("UNITS"); 
    String unit_price = rs.getString("UNIT_PRICE"); 

    Stock_Table.setValueAt(vendor,currentRow, 1); 
    Stock_Table.setValueAt(vendor,currentRow, 2); 
    Stock_Table.setValueAt(vendor,currentRow, 3); 
    Stock_Table.setValueAt(vendor,currentRow, 4); 
    currentRow+=1; 
} 
相關問題