她是我的測試JSP代碼和JavaBean DB函數代碼:如何在jsp中打印來自javabean的結果集數據?
的jsp:
<%
conn.init();
ResultSet rs = conn.selectProductById (request.getParameter("pid"));
while (rs.next()) {
System.out.println(rs.getString("pid"));
}
}
%>
的JavaBean:
公衆的ResultSet selectProductById(字符串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;
}
錯誤:
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的方法獲取結果集,但出現錯誤。如何解決它?
感謝
如果javaBean是應用程序範圍,如果包含結果集的對象會消耗太多資源嗎? – hkguile 2012-02-20 03:02:32