如何在jsp中从javabean打印结果集数据?
她是我的测试jsp代码和javabean db函数代码: jsp: 错误:如何在jsp中从javabean打印结果集数据?,jsp,javabeans,Jsp,Javabeans,她是我的测试jsp代码和javabean db函数代码: jsp: 错误: 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
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方法获取结果集,但出现了一个错误。如何修复它
谢谢您正在关闭/处理结果集/语句/连接对象,因此您需要返回
产品
对象引用或列表
,而不是结果集
比如说,
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;
}
public List selectProductById(字符串pid){
...
列表=新的ArrayList();
试一试{
String query=“从产品中选择*,其中pid=?”;
.....
while(rs.next())
{
产品项=新产品();
项目.setId(rs.getInt(1));
item.setName(rs.getString(2));
列表。添加(项目);
}
....
退货清单;
}
在finally块中,您正在关闭结果集和连接对象,然后返回resultset obj。尝试在Try块中返回resultset对象。如果javaBean是应用程序范围,那么包含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;
}