晕,我这个递归问题出在哪里?

ykb 2005-04-26 10:28:58
public String Tree(String strParent,int intLevel){


try{
if (conn==null)
{
conn=getConn();
}
String sql,img,tmp;
ResultSet rs=null;
sql="select * from type where parent_id="+ strParent +" order by category_order";

Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
//rs=conn.executeQuery(sql);
if (rs.next()){
while (rs.next())
{
if (intLevel==0){
img="<img src=images/bookOpen.gif>";
}else{
img="<img src=images/paper.gif>";
}
strTree=strTree+img+rs.getString(2)+"<br>";

strTree=strTree+Tree(rs.getString(4),intLevel+1);
}
}
rs.close();
rs=null;
}
catch(SQLException ex){
System.err.println("Database.executeQuery: " + ex.getMessage());
}
return strTree;
}
...全文
244 23 打赏 收藏 转发到动态 举报
写回复
用AI写文章
23 条回复
切换为时间正序
请发表友善的回复…
发表回复
ykb 2005-04-29
  • 打赏
  • 举报
回复
问题解决了,装了sql server jdbc的驱动就可以了 散分
再次感谢各位的帮助
fengyue2001 2005-04-28
  • 打赏
  • 举报
回复
出什么错误?估计去掉上面的if还会出同样的错误,你把错误报告拿出来看看
rower203 2005-04-28
  • 打赏
  • 举报
回复
此方法中的strTree用局域变量,而不用类变量,这样不知能否解决问题。
MARS.nEIL 2005-04-28
  • 打赏
  • 举报
回复
为什么:parent_id的要有不等于的strParent的可能..不然就跳不出?
-------------------------------
不等于的话,就是找不到符合条件的记录,那么函数就会结束,而不会继续递归,同样的,如果提前不等于的话,就不能取出全部的记录,实际上这已经是一个链了,中间只要一断就会继续下去了.
另外,正如楼上说的,去掉if语句,不然就是隔记录取出来.
qingzhuang 2005-04-28
  • 打赏
  • 举报
回复
if (rs.next()){
while (rs.next())
去掉if(rs.next())
ykb 2005-04-28
  • 打赏
  • 举报
回复
报错是:[Microsoft][ODBC SQL Server Driver]连接占线导致另一个 hstmt
我用的是jdbc-odbc访问SQL Server是不是一定要装SQL Server的jdbc驱动才能解决这个问题?
cnidb 2005-04-27
  • 打赏
  • 举报
回复
mark
MARS.nEIL 2005-04-27
  • 打赏
  • 举报
回复
strTree=strTree+Tree(rs.getString(4),intLevel+1);
你要保证sql="select * from type where parent_id="+ strParent +" order by category_order";这一语句中的parent_id的要有不等于的strParent的可能..不然就跳不出了.
还有,正如 jihanzhong(逍遥) 所说的,最好在外边建立conn,stmt,rs
topil 2005-04-27
  • 打赏
  • 举报
回复
public boolean next()
throws SQLException
Moves the cursor down one row from its current position. A ResultSet cursor is initially positioned before the first row; the first call to the method next makes the first row the current row; the second call makes the second row the current row, and so on.
if (rs.next()) while (rs.next()) 这两句重复了

然后Debug一下,看看是否死循环
ykb 2005-04-27
  • 打赏
  • 举报
回复
问题好像是出在,在递归中再次调用while (rs.next())的时候就出错了,这是为什么?
难道是分不清rs是这次的还是上次的?
dj0517 2005-04-27
  • 打赏
  • 举报
回复
up
silverend 2005-04-27
  • 打赏
  • 举报
回复
if (rs.next()) //将这行去掉

while (rs.next()) //判断是否继续循环下去时已将rs的游标下移了,所以上面的是多余的
ykb 2005-04-27
  • 打赏
  • 举报
回复
有3条记录,怎么看strTree的生命周期?
xiaozhen 2005-04-27
  • 打赏
  • 举报
回复
先确定你的sql执行后有几条结果,然后看你的strTree的生命周期?
bp69 2005-04-27
  • 打赏
  • 举报
回复
先确定是不是死循环,rs里面添加几条记录,debug一下
ykb 2005-04-27
  • 打赏
  • 举报
回复
稍做修改后只能出来一条记录,这是怎么回事,请各位再指点一下

public String Tree(String strParent,int intLevel){


try{
if (conn==null)
{
conn=getConn();
}
String sql,img,tmp;
ResultSet rs=null;
sql="select * from category where parent_id="+ strParent +" order by category_order";

Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
//rs=conn.executeQuery(sql);
while (rs.next()){
if (intLevel==0){
img="<img src=images/bookOpen.gif>";
}else{
img="<img src=images/paper.gif>";
}
strTree=strTree + img+rs.getString(2)+"<br>";
strTree=strTree + Tree(rs.getString(1),intLevel+1);
}
rs.close();
rs=null;
}
catch(SQLException ex){
System.err.println("Database.executeQuery: " + ex.getMessage());
}
return strTree;
}
txranger 2005-04-27
  • 打赏
  • 举报
回复
You didn't say what problem you encountered. Infinite loop?

Looks like you are counting on the "select" returning empty rows to terminate the recursion. It's kind of dangerous because your program will then depend on the data's integrity.

For better performance, you might want to define a PreparedStatement outside of the recusion and for every iteration, just set a different strParent. You also want to pass in the Connection so you don't have to call getConnection for every iteration.
s3x4 2005-04-27
  • 打赏
  • 举报
回复
if (rs.next()){
while (rs.next())

这里连续2个next(),第一个结果不要了么?
ykb 2005-04-27
  • 打赏
  • 举报
回复
为什么:parent_id的要有不等于的strParent的可能..不然就跳不出?
还有怎么在外边建立conn,stmt,rs?
我在外边建的在函数里都不能调用
各位能否说的详细点,偶刚接触java
jihanzhong 2005-04-26
  • 打赏
  • 举报
回复
还有,最好传连接进去,你这样每个都建连接很容易死的
加载更多回复(3)

81,094

社区成员

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

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