help!! recursive function call
[code]
/**
*The function which build the category tree
*/
public String categoryTree(Statement stat, boolean isMore, int id) {
if(!isMore) {
return "";
}
else
{
String sql = " select t_category_relation.category_relation_id, t_category_relation.level_in, " +
" t_category_item.category_item_id, t_category_item.category_name_jpn_NV from " +
" t_category_item, t_category_relation " +
" where " +
" t_category_item.category_item_id = t_category_relation.category_item_id and " +
" t_category_relation.parent_id = " + id + " and t_category_relation.parent_id<>0";
//return sql;
try{
ResultSet res = stat.executeQuery(sql);
String spacer = "";
String input = "";
while(res.next()) {
int level = res.getInt(2);
id = res.getInt(1);
for(int i = 0; i < level; i ++) {
spacer +=" ";
}
input ="+ id: " +id + " NAME " + res.getString(4) + "\n</td>\n<td align=center>\n<input type=checkbox value=" +String.valueOf(id) + " name=categoryid>\n</td>\n</tr>\n";
return "\t\t<TR>\n\t\t\t<TD>" + spacer + input + categoryTree(stat, true, id);
}
if(spacer.equals("")){
return input+categoryTree(stat, false, id);
}
}catch(SQLException e) {
return "SQL Exception";
}
}
return categoryTree(stat, false, id);
}
[code]
I am writing a menu generated base on a tree like relation ship that is store in a database. assume
vegetable has two child and one of the child has another child and so forth.
But I am getting a result like this:
vegetable-->
<1>childe
<1.1>childe
but missing <2>child
because the while loop doesn't continous looping after the 1.1.
please help me out
thanx in advance
When you call the method recursively, the second call makes a second query to the database, before you have finished using the ResultSet from the first query. Since you are using the same Statement, executing the second query causes the first query to, um, disappear.
The API documentation for java.sql.Statement says this: "Only one ResultSet object per Statement object can be open at any point in time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All statement execute methods implicitly close a statment's current ResultSet object if an open one exists."
DrClap at 2007-6-29 10:48:23 >

[quote]
Re: help!! recursive function call
Author: DrClap Aug 3, 2001 1:15 PM
When you call the method recursively, the second call makes a second query to the database, before you have finished using the ResultSet from the first query. Since you are using the same Statement, executing the second query causes the first query to, um, disappear.
The API documentation for java.sql.Statement says this: "Only one ResultSet object per Statement object can be open at any point in time. Therefore, if the reading of one ResultSet object is interleaved with the reading of another, each must have been generated by different Statement objects. All statement execute methods implicitly close a statment's current ResultSet object if an open one exists."
[quote]
thanx for your reply!
[code]
public String categoryTree(int id) {
String sql = " select t_category_relation.category_relation_id, t_category_relation.level_in, " +
" t_category_item.category_item_id, t_category_item.category_name_jpn_NV from " +
" t_category_item, t_category_relation " +
" where " +
" t_category_item.category_item_id = t_category_relation.category_item_id and " +
" t_category_relation.parent_id = " + id;
try{
Connection con = DataSourceUtil.getConnection("name");
Statement stat = con.createStatement();
ResultSet res = stat.executeQuery(sql);
String spacer = "";
String row = "";
while(res.next()) {
int level = res.getInt(2);
id = res.getInt(1);
for(int i = 0; i < level; i++) {
spacer +=" ";
}
row = " \t\t<tr>\n " +
" \t\t\t<td colspan='2'>" + spacer + " <a href=inventory_edit_product.jsp?categoryid=" + id +">" + res.getString(4) + "</a></td>\n" +
" \t\t</tr>\n ";
return (row + categoryTree(id));
}
con.close();
}catch(SQLException e) {
return "<tr><td colspan=2>SQL Exception</td></tr>";
}
return "";
}
[code]
New I think every recursive call will have it's own statement and resultSet but I am still getting same problem. The while loop stopped when calls reached first base case. Does anybody know why. I expect, assume that while loop will go next when a call reaches the base case which will return "".
Thanx for help