Calculate Sum of row from jtable

I am using jtable and rs2xml.jar library

My table has 3 column. id,name, amount i want to calculate the sum of amount column.

here is the code:

    //showcal is my table name
       try {

            Connection conn = getConnection();

            PreparedStatement ps
                    = conn.prepareStatement("select id,name,amount from income where idate=?");
      ps.setString(1,((JTextField) inpdatechosser.getDateEditor().getUiComponent()).getText());
            rset = ps.executeQuery();
            showcal.setModel(DbUtils.resultSetToTableModel(rset));


 //sum calculation
int total = 0;

    for (int i = 0; i < showcal.getRowCount(); i++){
        int amount = Integer.parseInt( showcal.getValueAt(i, 3).toString());
        total =total+ amount;
    }

jTextField1.setText(""+Integer.toString(total));

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }

but nothing happen. i am getting "3>=3" whats that mean?? why it is not working??

Upvotes: 0

Views: 945

Answers (1)

tenorsax
tenorsax

Reputation: 21223

The index of rows and columns in the table is zero based. So the index of the 3rd column should be 2, ie: showcal.getValueAt(i, 2).

The exception you are getting means that the index of the column should be less than the number of columns.

Upvotes: 1

Related Questions