<%!
String result = " ";
private void showTree ( int id , Statement stmt) {
try {
ResultSet rs = stmt.executeQuery("select * from article where pid =" + id);

while  ( rs.next() ) {
result += "<tr><td>"+rs.getInt("id")+"</td><td>"+rs.getString("title")+"</td></tr>";
if ( rs.getInt("isleaf") != 0 ) {
showTree( rs.getInt("id"), stmt );
}
}
}  catch ( SQLException e ) {
 e.printStackTrace();
}
}private void showTree ( int id , Connection conn) {
try {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from article where pid =" + id);

while  ( rs.next() ) {
result += "<tr><td>"+rs.getInt("id")+"</td><td>"+rs.getString("title")+"</td></tr>";
if ( rs.getInt("isleaf") != 0 ) {
showTree( rs.getInt("id"), conn );
}
}
}  catch ( SQLException e ) {
 e.printStackTrace();
}
}
%>以上在两个方法你们觉得运行结果会一样吗??、 `````<%
Class.forName("oracle.jdbc.driver.OracleDriver");
String  connStr = "jdbc:oracle:thin:@localhost:1521:orcl";
Connection conn = DriverManager.getConnection( connStr ,"scott","tiger" );
// "jdbc:oracle:thin:@localhost:1521:orcl","scott","tiger"
Statement  stmt = conn.createStatement(); ResultSet  rls =  stmt.executeQuery("select *  from article where pid = 0 ");
   while ( rls.next() ) {
result += "<tr><td>"+rls.getInt("id")+"</td><td>"+rls.getString("title")+"</td></tr>";
if ( rls.getInt("isleaf") != 0  ) {
showTree ( rls.getInt("id") ,  stmt );
//showTree ( rls.getInt("id") ,  conn );
}

}   
%>
运行后结果不一样,怎么回事~!!!!奇怪~!!用传入Connection conn 的方法
用传入Statement stmt的方法:
感觉Statement stmt被多次调用时  Resultset rs 被改变了, 但是不可能这样。  我在另一个测试中试过没问题,  是不是递归的问题引起???   好奇怪,明明应该是一样的JavaOracle

解决方案 »

  1.   

    这是JavaDoc中的一句话
    在默认情况下,同一时间每个 Statement 对象在只能打开一个 ResultSet 对象。因此,如果读取一个 ResultSet 对象与读取另一个交叉,则这两个对象必须是由不同的 Statement 对象生成的。如果存在某个语句的打开的当前 ResultSet 对象,则 Statement 接口中的所有执行方法都会隐式关闭它。目测LZ的查询结果中有2个叶子节点,由于第二个showTree每次都创建了新的Statement,所以出现了上面的结果