Reputation: 331
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
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
Reputation: 6844
I decided to sum up my observations which I mentioned above in comments.
Your code had a couple issues
i++
and i=i-1
which makes the variant of the cycle to be invariant, so your loop is endless (you already fixed that)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.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:
for
cyclebased 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