learner
learner

Reputation: 331

How can i update table model multiple row columns?

I have 3 rows in tablemodel.each row conatining 9 columns (o-8).i want to update last column of every row...but now i can update only 1st rowth last column..pls help..`

   ` SAVE=new JButton("SAVE");
     SAVE.addActionListener(new java.awt.event.ActionListener() {  
      public void actionPerformed(java.awt.event.ActionEvent e) {  
      String Status=null ;
      DbUtility ViewAbsenties=new DbUtility();
      ViewAbsenties. loadDriver();
      ViewAbsenties.connect();
      TableModel tm;
      tm = table3.getModel();
      int i = table3.getRowCount();
      System.out.println("row count"+i);

          try{
            while(i!=0){
              Status   =(String) tm.getValueAt(0,8);
              String Employeeid = (String) tm.getValueAt(i,0);
              System.out.println("Status: " + Status);
              System.out.println("Employeeid: " + Employeeid);
              ViewAbsenties.executeUpdateQuery2(Status,Employeeid);
              System.out.println("i"+i);
               i=i-1;

                  }}
            catch (Exception e5) {

                }
          });   

Upvotes: 1

Views: 741

Answers (2)

Farnabaz
Farnabaz

Reputation: 4066

You should use

Status   =(String) tm.getValueAt(i,8);
String Employeeid = (String) tm.getValueAt(i,0);

instead of

Status   =(String) tm.getValueAt(0,8);
String Employeeid = (String) tm.getValueAt(0,0);

first argument in getValueAt is rowIndex, and you should pass indexes of rows you want to update but you only update row 0 in loop.

Upvotes: 2

Gaim
Gaim

Reputation: 6844

I decided to sum up my observations which I mentioned above in comments.

Your code had a couple issues

  • in the same loop cycle you call i++ and i=i-1 which makes the variant of the cycle to be invariant, so your loop is endless (you already fixed that)
  • you are still calling tm.getValueAt(0,8). The first parameter is rowIndex, the second is a columnIndex. So for each iteration you work still with the first row, not with the current one.
  • Java usually indexes models and arrays from 0 to length - 1 but you are iterate from length to 1. So you need to lower the variable i by 1

There is fixed code:

int i = table3.getRowCount() - 1;
while( i >= 0 ){

          Status = (String) tm.getValueAt( i, 8 );
          String Employeeid = (String) tm.getValueAt( i, 0 );

          System.out.println( "Status: " + Status );
          System.out.println( "Employeeid: " + Employeeid );

          ViewAbsenties.executeUpdateQuery2( Status, Employeeid );

          System.out.println( "i" + i );
          i = i - 1;
}

Note: Anyway there are my few observations to improve your code style:

  • the example above is the exact use case for for cycle
  • based on Java convention variable names should start with a non-capital letter.

    for ( int i = 0; i < table3.getRowCount(); ++i ) {
          status = (String) tm.getValueAt( i, 8 );
          String employeeid = (String) tm.getValueAt( i, 0 );
    
          System.out.println( "Status: " + status );
          System.out.println( "Employeeid: " + employeeid );
    
          ViewAbsenties.executeUpdateQuery2( status, employeeid );
    
          System.out.println( "i" + i );
    }
    

Upvotes: 1

Related Questions