[英]Only Last Value is Printing From Database in 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);
}
首先,JavaDocs清楚地表明setValueAt(Object, int, int)
这两个int值分别表示该行和列。
因此,根据您的示例,您将更新第1、2、3和4行的第一列。
其次,您应避免为此使用setValueAt
,而应将行添加到TableModel
看看如何使用表格了解更多详细信息
尝试给增量行像下面的代码。
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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.