2017-02-10 45 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

表中的行和列的索引是基於零的。所以第三列的索引應該是2,即:showcal.getValueAt(i, 2)

您所遇到的異常意味着列的索引應小於列數。

+1

是的...我想念那個部分...謝謝 –