| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1481 人关注过本帖
标题:谁能帮我看看jsp页面为什么抛这个异常
只看楼主 加入收藏
LibRA_kin
Rank: 1
等 级:新手上路
帖 子:20
专家分:0
注 册:2008-11-29
结帖率:85.71%
收藏
已结贴  问题点数:10 回复次数:5 
谁能帮我看看jsp页面为什么抛这个异常
这是一个实现BBS帖子展现的jsp页面 可是每次用浏览器打开时会抛出:java.sql.SQLException: Operation not allowed after ResultSet closed
ShowArticleTree.jsp:<%@ page language="java" contentType="text/html; charset=GBK"
    pageEncoding="GBK"%>
<%@ page import="java.sql.*,com.libra.bbs.*,java.util.*"%>

<%
    String admin = (String) session.getAttribute("admin");
    if (admin != null && admin.equals("true")) {
        blogin = true;
    }
%>

<%!boolean blogin = false;

    private void tree(List<Article> articles, Connection conn, int id, int grade) {
        String sql = "select * from article where pid =" + id;

        Statement stmt = DBase.createStmt(conn);
        ResultSet rs = DBase.executeQuery(stmt, sql);
        try {
            while (rs.next()) {
                Article ar = new Article();
                ar.initFromRs(rs);
                ar.setGrade(grade);
                articles.add(ar);
                if (!ar.isLeaf()) {
                    tree(articles, conn, ar.getId(), grade + 1);
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBase.close(rs);
            DBase.close(stmt);
        }
    }%>
<%
    List<Article> articles = new ArrayList<Article>();
    Connection conn = DBase.getConn();
    tree(articles, conn, 0, 0);
    DBase.close(conn);
%>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>Insert title here</title>
</head>
<body>

<h1>BBS帖子输出</h1>
<a href="Post.jsp">发表主题</a>
<table border="1">

    <%
        for (Iterator<Article> it = articles.iterator(); it.hasNext();) {
            Article a = it.next();
            String preStr = "";
            for (int i = 0; i < a.getGrade(); i++) {
                preStr += "----";
            }
    %>


    <tr>
        <td><%=a.getId()%></td>
        <td><a href="ShowArticleDetail.jsp?id=<%=a.getId()%>"> <%=preStr + a.getTitle()%>
        </a></td>
        <%
            if (blogin) {
        %>
        <td><a href="Delete.jsp?id=<%=a.getId()%>&pid=<%=a.getPid()%>">删除</a></td>
        <%
            }
        %>
    </tr>
    <%
        }
    %>

</table>

</body>

</html>

用到的两个JavaBean类:

package com.libra.bbs;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;

public class Article {
    private int id;
    private int pid; //private Article parent
    private int rootId;
    private String title;
    private String cont;
    private Date pdate;
    private boolean isLeaf;
    private int grade;
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public int getPid() {
        return pid;
    }
    public void setPid(int pid) {
        this.pid = pid;
    }
    public int getRootId() {
        return rootId;
    }
    public void setRootId(int rootId) {
        this.rootId = rootId;
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getCont() {
        return cont;
    }
    public void setCont(String cont) {
        this.cont = cont;
    }
    public Date getPdate() {
        return pdate;
    }
    public void setPdate(Date pdate) {
        this.pdate = pdate;
    }
    public boolean isLeaf() {
        return isLeaf;
    }
    public void setLeaf(boolean isLeaf) {
        this.isLeaf = isLeaf;
    }
    public int getGrade() {
        return grade;
    }
    public void setGrade(int grade) {
        this.grade = grade;
    }
    
    public void initFromRs(ResultSet rs) {
        try {
            setId(rs.getInt("id"));
            setPid(rs.getInt("pid"));
            setRootId(rs.getInt("rootid"));
            setTitle(rs.getString("title"));
            setLeaf(rs.getInt("isleaf") == 0 ? true : false);
            setPdate(rs.getTimestamp("pdate"));
            setCont(rs.getString("cont"));
            setGrade(0);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}


package com.libra.bbs;

import java.sql.*;

public class DBase {
    public static Connection getConn(){
        Connection conn = null;
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bbs", "root" , "root");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn ;
    }
    
    public static Statement createStmt(Connection conn){
        Statement stmt = null ;
        try{
            stmt = conn.createStatement();
        }catch(SQLException e){
            e.printStackTrace();
        }
        return stmt ;
    }
    
    public static PreparedStatement createPstmt(Connection conn,String sql){
        PreparedStatement pStmt = null ;
        try{
            pStmt = conn.prepareStatement(sql);
        }catch(SQLException e){
            e.printStackTrace();
        }
        return pStmt ;
    }
    public static int executeUpdate(Connection conn, String sql) {
        int ret = 0;
        Statement stmt = null;
        try {
            stmt = conn.createStatement();
            ret = stmt.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            close(stmt);
        }
        return ret;
    }
    public static ResultSet executeQuery(Statement stmt,String sql){
        ResultSet rs = null ;
        try{
            
            rs = stmt.executeQuery(sql);
        }catch(SQLException e ){
            e.printStackTrace();
        }finally{
            close(stmt);
        }
        return rs ;
    }
    
    public static void close(Connection conn){
        if(conn!= null){
            try{
                conn.close();
                
            }catch(SQLException e){
                e.printStackTrace();
            }
            conn = null ;
        }
    }
    
    public static void close(Statement stmt){
        if(stmt!= null){
            try{
                stmt.close();
                
            }catch(SQLException e){
                e.printStackTrace();
            }
            stmt = null ;
        }
    }
    
    public static void close(ResultSet rs){
        if(rs!= null){
            try{
                rs.close();
                
            }catch(SQLException e){
                e.printStackTrace();
            }
            rs = null ;
        }
    }
}
搜索更多相关主题的帖子: jsp JavaBean 
2009-07-24 16:36
zhong0711101
Rank: 2
等 级:论坛游民
帖 子:156
专家分:25
注 册:2008-7-15
收藏
得分:2 
主要是Operation not allowed after ResultSet closed

学完Java ,还有很长的路要走!
2009-07-27 16:51
ceco3000
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:61
专家分:147
注 册:2009-7-14
收藏
得分:2 
我今天写JSP碰上了和你一样的问题
使用一个方法返回查询的RS就异常.
写到一起就好了迷糊了
2009-07-27 19:14
田代印
Rank: 2
来 自:山东
等 级:论坛游民
帖 子:16
专家分:10
注 册:2009-8-3
收藏
得分:2 
ResultSet 这个类关闭了,在配置文件里面配置一下就好了!
2009-08-06 14:34
scqxzg
Rank: 2
等 级:论坛游民
帖 子:14
专家分:52
注 册:2009-8-5
收藏
得分:2 
同意四楼的说法, 把resultset关闭试试。

淘宝购  http://www.  淘宝网合作伙伴,全场低至二折!

2009-08-07 09:07
jackeysion
Rank: 2
等 级:论坛游民
帖 子:36
专家分:10
注 册:2007-6-16
收藏
得分:2 
public void initFromRs(ResultSet rs) {
        try {
            setId(rs.getInt("id"));
            setPid(rs.getInt("pid"));
            setRootId(rs.getInt("rootid"));
            setTitle(rs.getString("title"));
            setLeaf(rs.getInt("isleaf") == 0 ? true : false);
            setPdate(rs.getTimestamp("pdate"));
            setCont(rs.getString("cont"));
            setGrade(0);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
在finally中加个rs.close()试试

2009-08-10 18:19
快速回复:谁能帮我看看jsp页面为什么抛这个异常
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.026627 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved