I would appreciate it if someone could help me with my question.

Eventhought I was luck enough to get problem solved but I am still wonder about why my first recursive

and second recurive function didn't work as what I expected. I would appreciate it if someone

could help me with my question.

[code]

public String categoryTree(Statement stat, 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 + " and t_category_relation.parent_id<>0";

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, id));}}catch(SQLException e) {return "<tr><td colspan=2>SQL Exception</td></tr>";}return "<font size=3>" + id + "</font>";}[code][quote]Re: help!! recursive function call Author: DrClap Aug 3, 2001 1:15 PMWhen 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 f

Re: I need more reply on recursive, please coming

Author: jschell Aug 4, 2001 7:23 AM

What does 'stop' mean? Did it throw an exception or did it block?

If it blocked how do you know that that is what happened?

Re: I need more reply on recursive, please coming

Author: liberticide Aug 4, 2001 9:13 AM

What i mean is if a parent has two children the while loop stopped when first child reached return "";

I expect the while loop will go to next resultset and find

out next's children.

7

891011

2[code]

my recusive function only returns 7-->8-->2.

The while loop suppose go to 9 when 2 reached return "";

Re: I need more reply on recursive, please coming

Author: liberticide Aug 4, 2001 9:26 AM

Here is my working version of recursive,

but I would like to have a further discussion

about different between this and the one above. Please help

thx in advancen

public String categoryTree( int id, String tree, int level) {

String output_String = tree;

try{

Connection con = DataSourceUtil.getConnection( "name" );

ResultSet res = null;

Statement stat = null;

String sql = " select 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";

stat = con.createStatement();

res = stat.executeQuery( sql );

while ( res.next() ) {

String spacer = "";

for(int i = 0; i < level; i++) {

spacer +=" ";

}

id = res.getInt(1);

output_String = output_String + "\t\t<TR>\n\t\t\t<TD>" + spacer

+ "id :" + id + res.getString(2)

+ "\n</td>\n<td align=center>\n<input type=checkbox value=" +String.valueOf(id)

+ " name=categoryid>\n</td>\n</tr>\n";

output_String = categoryTree(id, output_String,level+1);

}

stat.close();

con.close();

}catch ( SQLException e ) {}

catch ( Exception e ) {}

return output_String;

}

Re: I need more reply on recursive, please coming

Author: liberticide Aug 4, 2001 9:28 AM

7

891011

2[code]the working version return me7-->8,9,10,11and 2

Re: I need more reply on recursive, please coming

Author: bobd3 Aug 4, 2001 10:51 AM

Are you getting the "same" connection from your DataSourceUtil class or are

you getting a different connection each time you make the call to the class.

If it's the same one then you are closing the connection in one of the

recursive calls before the original call to the method has a chance to finish with it's resultset.

Re: I need more reply on recursive, please coming

Author: liberticide Aug 4, 2001 10:55 AM

DataSourceUtil will return a connection from the pool.

The result won't be difference even not closing the connection or statement. Only for resource sake.

Thx u very much

[code]

[7553 byte] By [liberticide] at [2007-9-26 3:07:45]
# 1

Sorry about the mess, here is the code for the first recusive function.

thx in advance

public String categoryTree(Statement stat, 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 + " and t_category_relation.parent_id<>0";

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, id));

}

}catch(SQLException e) {

return "<tr><td colspan=2>SQL Exception</td></tr>";

}return "";

}

liberticide at 2007-6-29 11:11:48 > top of Java-index,Core,Core APIs...