【问题标题】:Only Last Value is Printing From Database in jTable只有最后一个值是从 jTable 中的数据库打印
【发布时间】:2014-04-22 05:52:15
【问题描述】:

我要将数据库中的股票记录显示到 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);
}

【问题讨论】:

  • Stock_Table 是什么?
  • 为了尽快获得更好的帮助,请发布MCVE(最小完整且可验证的示例)。

标签: java database swing jdbc jtable


【解决方案1】:

首先,JavaDocs 清楚地表明setValueAt(Object, int, int) 这两个 int 值按该顺序表示行和列。

因此,根据您的示例,您正在更新第 1、2、3 和 4 行的第一列。

其次,您应该避免为此使用setValueAt,而是将行添加到TableModel

查看How to Use Tables了解更多详情

【讨论】:

    【解决方案2】:

    尝试像下面的代码一样给出增量行。

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 2022-01-23
      • 2017-12-27
      • 2015-06-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-07
      • 2015-08-10
      相关资源
      最近更新 更多