如何在jsp中打印javabean的结果集数据?

发布于 2025-01-07 03:52:35 字数 1476 浏览 3 评论 0原文

她是我的测试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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

卷耳 2025-01-14 03:52:35

您正在关闭/处置结果集/语句/连接对象,因此需要返回 Product 对象引用或 List 而不是 ResultSet

例如,

public class Product
{
   private int id;
   private String name;
   .....
}

............

public List<Product> selectProductById (String pid) {

 ...
 List<Product> list=new ArrayList<Product>();

 try {
   String query = "select * from product where pid = ?";
   .....
   while(rs.next())
   {
      Product item=new Product();
      item.setId(rs.getInt(1));
      item.setName(rs.getString(2));
      list.add(item);
    } 
  ....
  return list;
}

You are closing/disposing the resultset/statement/connection object so you need to return Product object reference or List<T> instead of ResultSet.

For instance,

public class Product
{
   private int id;
   private String name;
   .....
}

............

public List<Product> selectProductById (String pid) {

 ...
 List<Product> list=new ArrayList<Product>();

 try {
   String query = "select * from product where pid = ?";
   .....
   while(rs.next())
   {
      Product item=new Product();
      item.setId(rs.getInt(1));
      item.setName(rs.getString(2));
      list.add(item);
    } 
  ....
  return list;
}
赠意 2025-01-14 03:52:35

在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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文