从 jtable 计算行的总和

Calculate Sum of row from jtable

我正在使用 jtable 和 rs2xml.jar 库

我的 table 有 3 列。 id,name,amount 我想计算金额列的总和。

代码如下:

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

但什么也没发生。我收到“3>=3” 那是什么意思??为什么它不起作用??

table中行和列的索引是从零开始的。所以第3列的索引应该是2,即:showcal.getValueAt(i, 2)

你得到的异常意味着列的索引应该小于列数。