如何使用 JDBC 打印 SUM 和 AVG SQL 值?

发布于 2024-11-01 22:49:02 字数 767 浏览 0 评论 0原文

好的,我正在尝试使用 JDBC 打印 SUM 和 AVG 函数的结果。所以基本上,它不起作用。我做错了什么?

   <table border="1">
     <tr><th>Total Homes Sold</th><th>Total Sales Amount</th><th>Averages Price Per Home</th></tr>    

       <% rset = stmt.executeQuery("SELECT COUNT(home_ID) FROM home");%>
       <% rset1 = stmt.executeQuery("SELECT SUM(purchase_Price) FROM home");%>          
       <% rset2 = stmt.executeQuery("SELECT AVG(purchase_Price) FROM home");%>

       <tr>
         <td><%= rset.getString(1) %></td>
         <td><%= rset1.getString(1) %></td>
         <td><%= rset2.getString(1) %></td>
      </tr>

   </table>

Okay, I am trying to do print the results of a SUM and AVG function using JDBC. So basically, it's not working. What am I doing wrong?

   <table border="1">
     <tr><th>Total Homes Sold</th><th>Total Sales Amount</th><th>Averages Price Per Home</th></tr>    

       <% rset = stmt.executeQuery("SELECT COUNT(home_ID) FROM home");%>
       <% rset1 = stmt.executeQuery("SELECT SUM(purchase_Price) FROM home");%>          
       <% rset2 = stmt.executeQuery("SELECT AVG(purchase_Price) FROM home");%>

       <tr>
         <td><%= rset.getString(1) %></td>
         <td><%= rset1.getString(1) %></td>
         <td><%= rset2.getString(1) %></td>
      </tr>

   </table>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

沉鱼一梦 2024-11-08 22:49:02

主要问题是您需要在结果集上调用 next() (在 executeQuery() 之后)来检索结果集的第一行(即使只有一排)。

如果您不这样做,则没有可用行,并且调用 getString() 会导致“ResultSet 已关闭”错误。

The main problem is that you need to call next() on the result set (after executeQuery()) to retrieve the first row of the result set (even though there's just one row).

It you don't do it, no row is available and calling getString() causes the "ResultSet is closed" error.

那伤。 2024-11-08 22:49:02

尝试这样做:

   <tr>
   <% rset = stmt.executeQuery("SELECT COUNT(home_ID) FROM home");%>
     <td><%= rset.getString(1) %></td>
   <% rset = stmt.executeQuery("SELECT SUM(purchase_Price) FROM home");%>          
     <td><%= rset.getString(1) %></td>
   <% rset = stmt.executeQuery("SELECT AVG(purchase_Price) FROM home");%>
     <td><%= rset.getString(1) %></td>
  </tr>

或者更好:

   <tr>
   <% rset = stmt.executeQuery("SELECT COUNT(home_ID), SUM(purchase_Price), AVG(purchase_Price) FROM home");%>
     <td><%= rset.getString(1) %></td>
     <td><%= rset.getString(2) %></td>
     <td><%= rset.getString(3) %></td>
  </tr>

Try doing:

   <tr>
   <% rset = stmt.executeQuery("SELECT COUNT(home_ID) FROM home");%>
     <td><%= rset.getString(1) %></td>
   <% rset = stmt.executeQuery("SELECT SUM(purchase_Price) FROM home");%>          
     <td><%= rset.getString(1) %></td>
   <% rset = stmt.executeQuery("SELECT AVG(purchase_Price) FROM home");%>
     <td><%= rset.getString(1) %></td>
  </tr>

Or better:

   <tr>
   <% rset = stmt.executeQuery("SELECT COUNT(home_ID), SUM(purchase_Price), AVG(purchase_Price) FROM home");%>
     <td><%= rset.getString(1) %></td>
     <td><%= rset.getString(2) %></td>
     <td><%= rset.getString(3) %></td>
  </tr>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文