user3487666
user3487666

Reputation: 9

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

Upvotes: 0

Views: 70

Answers (2)

Hashir Sheikh
Hashir Sheikh

Reputation: 1821

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

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347184

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

Upvotes: 4

Related Questions