【问题标题】:JDBC - How do I accumulate data from ResultSetJDBC - 如何从 ResultSet 中累积数据
【发布时间】:2013-06-26 21:00:19
【问题描述】:

如何从 ResultSet 中累积数据?我要做的是从 ResultSet 中获取价格,它只获取第一个值,但我想要的是每个价格,例如 25.0、10.00 和 15.00 我已将它们存储在 Double 数据类型中,但结果集只有当我有一个 while(rs.next() 并且如果我有 if(rs.next()) 它将获得 25.00 但我想要所有要添加的价格。我该怎么做呢?谢谢!

if(rs2.next()) {

           jTextArea3.append("\n");
           jTextArea3.append("\n");
           jTextArea3.append("Part Type: " + rs2.getString("PartType"));
           jTextArea3.append("\n");
           jTextArea3.append("Description: " + rs2.getString("Description"));
           jTextArea3.append("\n");
           jTextArea3.append("Price: " + rs2.getString("Price"));
           partsCost = rs2.getDouble("Price");
           System.out.println(partsCost);
      }  

【问题讨论】:

    标签: jdbc


    【解决方案1】:

    也许你的意思是这样的?

    double total = 0;
    
    while(rs.next()) {
    
               jTextArea3.append("\n");
               jTextArea3.append("\n");
               jTextArea3.append("Part Type: " + rs2.getString("PartType"));
               jTextArea3.append("\n");
               jTextArea3.append("Description: " + rs2.getString("Description"));
               jTextArea3.append("\n");
               jTextArea3.append("Price: " + rs2.getString("Price"));
               partsCost = rs2.getDouble("Price");
               System.out.println(partsCost);
    
               total += partsCost;
    
          }  
    
    System.out.println(total);
    

    您没有显示 SQL 查询。如果您只关心partsCosts 的总数,而不关心其他详细信息,则需要将查询更改为具有聚合函数,例如“select sum(partsCost)...”

    【讨论】:

      猜你喜欢
      • 2014-06-25
      • 1970-01-01
      • 2011-02-06
      • 1970-01-01
      • 2019-06-06
      • 1970-01-01
      • 2018-07-19
      • 2022-01-24
      • 1970-01-01
      相关资源
      最近更新 更多