好的,我正在尝试使用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>
答案 0 :(得分:3)
主要问题是你需要在结果集上调用 next()(在 executeQuery()之后)来检索结果集的第一行(甚至虽然只有一行)。
你没有这样做,没有行可用,并且调用 getString()导致&#34; ResultSet关闭&#34;错误。
答案 1 :(得分:1)
尝试做:
<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>