如何使用 JDBC 打印 SUM 和 AVG SQL 值?
好的,我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
主要问题是您需要在结果集上调用 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.
尝试这样做:
或者更好:
Try doing:
Or better: