[英]Why do I get an error when trying to sum values of a column retrieved in a 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.