如何在jsp中打印javabean的结果集数据?
她是我的测试jsp代码和javabean db功能代码:
jsp:
<%
conn.init();
ResultSet rs = conn.selectProductById (request.getParameter("pid"));
while (rs.next()) {
System.out.println(rs.getString("pid"));
}
}
%>
javabean:
public ResultSet selectProductById (String pid) {
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
String query = "select * from product where pid = ? ;";
pstmt = connection.prepareStatement(query); // create a statement
pstmt.setString(1, pid); // set input parameter
System.out.println(pstmt);
rs = pstmt.executeQuery();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
pstmt.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return rs;
}
error:
type Exception report
message
descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: java.sql.SQLException: Operation not allowed after ResultSet closed
root cause
java.sql.SQLException: Operation not allowed after ResultSet closed
note The full stack traces of the exception and its root causes are available in the GlassFish Server
jsp代码尝试从javabean的方法获取结果集,但出现错误。如何修复它?
谢谢
her is my test jsp code and javabean db function code:
jsp:
<%
conn.init();
ResultSet rs = conn.selectProductById (request.getParameter("pid"));
while (rs.next()) {
System.out.println(rs.getString("pid"));
}
}
%>
javabean:
public ResultSet selectProductById (String pid) {
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
String query = "select * from product where pid = ? ;";
pstmt = connection.prepareStatement(query); // create a statement
pstmt.setString(1, pid); // set input parameter
System.out.println(pstmt);
rs = pstmt.executeQuery();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
rs.close();
pstmt.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
return rs;
}
error:
type Exception report
message
descriptionThe server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: java.sql.SQLException: Operation not allowed after ResultSet closed
root cause
java.sql.SQLException: Operation not allowed after ResultSet closed
note The full stack traces of the exception and its root causes are available in the GlassFish Server
The jsp code is try to get the resultset from the method of javabean but there is an error. how to fix it?
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在关闭/处置结果集/语句/连接对象,因此需要返回
Product
对象引用或List
而不是ResultSet
。例如,
............
You are closing/disposing the resultset/statement/connection object so you need to return
Product
object reference orList<T>
instead ofResultSet
.For instance,
............
在finally 块中,您将关闭结果集和连接对象,然后返回结果集obj。尝试在 try 块中返回结果集对象。
In finally block, you are closing the result set and the connection objects and later returning the resultset obj. Try to return the resultset object in the try block.