为什么我会尝试总结在ResultSet检索列的值时,一个错误?(Why do I get an er

2019-10-23 01:44发布

什么是错我的代码? 我认为它的SQL ,但我不知道为什么。 先感谢您

try{
    clsConnect c = new clsConnect();
    Connection conn = c.makeConnection();
    Statement statement = conn.createStatement();
    String display = "SELECT SUM(Amountpaid) from mofficetbl";
    ResultSet rs = statement.executeQuery(display);
    while(rs.next()){
        totalTF.setText(rs.getString("Amountpaid"));
    }
}
catch (SQLException e) {
    JOptionPane.showMessageDialog(null, e);
}

Answer 1:

您选择SUM(amountpaid)并试图访问列amountpaid ,但该列实际上并不在存在ResultSet尝试rs.getString(1)或给SUM的名称在选择如SELECT SUM(amountpaid) as sum from msofficetbl然后做rs.getString("sum");



Answer 2:

当你调用这个select语句; 返回的列实际上是所谓的“SUM(amountpaid)”,而不是amountpaid所以要么改变其使用的别名

"SELECT SUM(Amountpaid) as Amountpaid from mofficetbl"

你也可以这样做:

rs.getString("Sum(Amountpaid)");


文章来源: Why do I get an error when trying to sum values of a column retrieved in a ResultSet?