简体   繁体   中英

Only Last Value is Printing From Database in jTable

I am going to show stock records from database into a jTable. But when i retrieve values from database to jTable it is showing only last value.

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

Firstly, the JavaDocs clearly show that setValueAt(Object, int, int) that the two int values represents the row and column in that order.

So based on your example, you are updating the first column of rows 1, 2, 3 and 4.

Secondly, you should avoid using setValueAt for this purpose and instead add the rows to the TableModel

Take a look at How to Use Tables for more details

try to give increment row like below code.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM