簡體   English   中英

嘗試對ResultSet中檢索到的列的值求和時為什么會出現錯誤?

[英]Why do I get an error when trying to sum values of a column retrieved in a ResultSet?

我的代碼有什么問題? 我認為它在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);
}

當您調用此選擇語句時; 返回的列實際上稱為“ SUM(amountpaid)”,而不是amountpaid,因此可以使用以下方式更改其別名:

"SELECT SUM(Amountpaid) as Amountpaid from mofficetbl"

您也可以這樣做:

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

您正在選擇SUM(amountpaid)並嘗試訪問amountpaid列,但該列實際上不存在於ResultSet嘗試rs.getString(1)或在select中為SUM取一個名稱作為SELECT SUM(amountpaid) as sum from msofficetbl然后執行rs.getString("sum");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM