SQL递归查询-——java.lang.StackOverflowError,求解!

a673921712 2013-08-15 12:26:29
目前在练习尚学堂马士兵BBS项目,在Eclipse中运行Tomcat,连接Mysql,在做树状结构展现时(jsp文件)发生java.lang.StackOverflowError。
在网上看了这个异常的发生原因,一种是递归程序陷入死循环,内存耗尽;一种是JVM内存过小,递归过度消耗内存。
对照视频中的代码,没发现我的代码有什么错误;检查Eclipse的eclipse.ini的配置文件,“Xms40m-Xmx512m”,内存也不小。
下面我把SQL语句和JSP代码贴出来,希望大家能帮我分析下原因,万分感谢。

SQL部分:

create database bbs;

use bbs;

create table article
(
id int primary key auto_increment, //帖子id
pid int, //回复帖子的id,0代表主题帖
rootid int,
title varchar(255),
cont text,
pdate datetime,
isleaf int //叶子节点,0表示叶子结点,1表示非叶子结点
);

use bbs;
insert into article values(null,0,1,"蚂蚁大战大象","蚂蚁大战大象",now(),1);
insert into article values(null,1,1,"大象被打趴下了","大象被打趴下了",now(),1);
insert into article values(null,2,1,"蚂蚁也不好过","蚂蚁也不好过",now(),0);
insert into article values(null,2,1,"瞎说","瞎说",now(),1);
insert into article values(null,4,1,"没有瞎说","没有瞎说",now(),0);
insert into article values(null,1,1,"怎么可能","怎么可能",now(),1);
insert into article values(null,6,1,"怎么没有可能","怎么没有可能",now(),0);
insert into article values(null,6,1,"可能性是很大的","可能性是很大的",now(),0);
insert into article values(null,2,1,"大象进医院了","大象进医院了",now(),1);
insert into article values(null,9,1,"护士是蚂蚁","护士是蚂蚁",now(),0);


JSP部分:

<%@ page language="java" contentType="text/html; charset=gbk"
pageEncoding="gbk"%>
<%@ page import="java.sql.*" %>

<%!
String str = "";
private void tree(Connection conn,int id,int level) {
Statement stmt = null;
ResultSet rs = null;
String preStr = ""; //前缀字符

for(int i=0;i<level;i++) {
preStr += "----";
}

try {
String sql = "select * from article where pid=" + id;
stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
while(rs.next()) {
str += "<tr><td>" + rs.getInt("id") + "</td><td>" +
preStr + rs.getString("title") + "</td></tr>";
if(rs.getInt("isleaf") != 0) { //不是叶子节点
tree(conn,rs.getInt(id),level+1);
}
}
} catch(SQLException e) {
e.printStackTrace();
} finally {
try {
if(rs != null) {
rs.close();
rs = null;
}

if(stmt != null) {
stmt.close();
stmt = null;
}
} catch(SQLException e) {
e.printStackTrace();
}
}
}
%>



<%
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/bbs?user=root&password=123";
Connection conn = DriverManager.getConnection(url);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from article where pid= 0");//主题帖结果集
while(rs.next()) { //展现主题贴下的回复帖
str += "<tr><td>" + rs.getInt("id") + "</td><td>" +
rs.getString("title") + "</td></tr>";
if(rs.getInt("isleaf") != 0) {
tree(conn,rs.getInt("id"),1);
}
}

rs.close();
stmt.close();
conn.close();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gbk">
<title>Insert title here</title>
</head>
<body>
<table border="1">
<%= str %>
</table>
</body>

</html>
...全文
201 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
末日哥 2013-08-15
  • 打赏
  • 举报
回复
拿到的是列的索引号。。不是列名
a673921712 2013-08-15
  • 打赏
  • 举报
回复
引用 2 楼 abstruct 的回复:
try {         String sql = "select * from article where pid=" + id;         stmt = conn.createStatement();         rs = stmt.executeQuery(sql);         while(rs.next()) {             str += "<tr><td>" + rs.getInt("id") + "</td><td>" +                    preStr + rs.getString("title") + "</td></tr>";             if(rs.getInt("isleaf") != 0) {       //不是叶子节点                 tree(conn,rs.getInt(id),level+1);             }         }     } catch(SQLException e) { 没加引号
太感谢您了,找了一整天都没发现问题。
安特矮油 2013-08-15
  • 打赏
  • 举报
回复
try {         String sql = "select * from article where pid=" + id;         stmt = conn.createStatement();         rs = stmt.executeQuery(sql);         while(rs.next()) {             str += "<tr><td>" + rs.getInt("id") + "</td><td>" +                    preStr + rs.getString("title") + "</td></tr>";             if(rs.getInt("isleaf") != 0) {       //不是叶子节点                 tree(conn,rs.getInt(id),level+1);             }         }     } catch(SQLException e) { 没加引号
我的波塞冬 2013-08-15
  • 打赏
  • 举报
回复
我猜是第一种吧

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧