彼女である私のテスト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のメソッドから結果セットを取得しようとしていますが、エラーがあります。どのようにそれを修正するには?あなたの代わりにResultSet
のProduct
オブジェクト参照またはList<T>
を返す必要があるので、あなたが/結果セット/声明を配置/接続オブジェクトをクローズしている
おかげ
javaBeanがアプリケーションスコープの場合、結果セットを含むオブジェクトの場合、それは多くのリソースを消費しますか? – hkguile