2017-02-10 20 views
0

私はjtableとrs2xml.jarライブラリを使用していますjtableからの行の合計を計算する

私のテーブルは3列です。 ID、名前、金額 金額の合計を計算したい。

//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"を得ています。 その意味は何ですか?なぜそれが動作していないのですか?

答えて

1

テーブルの行と列のインデックスはゼロベースです。したがって、3列目のインデックスは2でなければなりません。つまり、showcal.getValueAt(i, 2)です。

例外は、列のインデックスが列の数より少なくなることを意味します。

+1

はい...私はその部分を逃しています...ありがとう –

関連する問題