我正在构建一个3层
应用程序,它使用java
类从db
和jsp
检索数据用户界面的页面。当我尝试在 jsp 页面中调用返回结果集的方法时,收到以下错误消息:
Operation not allowed after ResultSet closed.
例如我有这样的方法:
public ResultSet getName(String country) throws SQLException {
String name;
try {
open();
stmt1 = con.prepareStatement(fetchPanepistimio);
stmt1.setString(1,country);
rs1 = stmt1.executeQuery();
stmt1.close();
close();
}catch(Exception e2) {
}
return rs1;
}
当我在jsp页面中编写以下内容时:
<%
ResultSet rs = panepistimio.getName(country);
while(rs.next()) { %>
...........
<% } %>
我得到了我所描述的异常。您知道出了什么问题吗?预先感谢您
最佳答案
您试图在关闭数据库连接stmt1.close();
和close();
)后访问ResultSet,但这种方式不起作用。 (有关详细说明,请参阅 Oracle's tutorial)。
您应该检索 ResultSet
的内容,然后在 JSP 页面中使用它,而不是让连接保持打开状态。
这样,您就可以将数据库查询的生命周期与 View 生命周期解耦。
关于java - 调用返回结果集的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34329979/