[英]Calculate Sum of row from jtable
我正在使用jtable和rs2xml.jar库
我的表有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”是什么意思? 为什么它不起作用?
表中行和列的索引从零开始。 因此,第三列的索引应为2,即: showcal.getValueAt(i, 2)
。
您得到的异常意味着该列的索引应小于列数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.