sbPre这个变量在递归的过程中,是怎么变化的?

2019-03-25 13:37|来源: 网路

import java.sql.*;

public class ArticleTree {

	public static void main(String[] args) {
		new ArticleTree().show();
	}
	
	private void show(){
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager
			.getConnection("jdbc:mysql://localhost/bbs?user=root&password=root");
			stmt = conn.createStatement();
			rs = stmt.executeQuery("select * from article where pid=0");
			while(rs.next()){
				System.out.println(rs.getString("cont"));
				tree(conn,rs.getInt("id"),1);
			}			
		} catch (ClassNotFoundException e) {			
			e.printStackTrace();
		} catch (SQLException e) {			
			e.printStackTrace();
		} finally {
			try {
				if (conn != null){
					conn.close();
					conn = null;
				}
				if (stmt != null){
					stmt.close();
					stmt = null;
				}
				if (rs != null){
					rs.close();
					rs = null;
				}
			} catch (SQLException e) {				
					e.printStackTrace();
			}			
		}
		
	}

	
	private void tree(Connection conn,int id,int level) {
		Statement stmt = null;
		ResultSet rs = null;
		String str = "select * from article where pid =";
		StringBuffer sbPre = new StringBuffer("");
		for(int i=0;i<level; i++){
			sbPre.append("    ");
		}
		try {
			stmt = conn.createStatement();
			rs = stmt.executeQuery(str + id);
			while(rs.next()){
				System.out.println(sbPre + rs.getString("cont"));
				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();
			}
		}		
		
		 
		
		
	}

}
下图是article这张表的数据和结构:
下图是程序执行的输出结果:

 

相关问答

更多
  • BEGIN IF (1 = 1) THEN DBMS_OUTPUT.PUT_LINE('这是第一层的if'); IF (1 = 1) THEN DBMS_OUTPUT.PUT_LINE('这是第二层的if'); END IF; ELSE DBMS_OUTPUT.PUT_LINE('这是第一层的else'); END IF; END; 这个是我测试的 不会被第一个if截断 是不是你脚本有问题?
  • 一个带参数的存储过程。 SQL> CREATE OR REPLACE PROCEDURE HelloWorld1 ( 2 p_user_name VARCHAR2 3 ) AS 4 BEGIN 5 dbms_output.put_line('Hello ' || p_user_name || '!'); 6 END HelloWorld1; 7 / Procedure created. SQL> CREATE OR REPLACE PROCEDURE CallHelloWorld1 ( 2 p_user V ...
  • 用动态sql: SET @sql = CONCAT('drop table test_',year(now()),month(now())); PREPARE s1 FROM @sql; EXECUTE s1;
  • SQL>!'. SQL> )一个带参数的存储过程; Procedure created;; Procedure created.put_line(' 6 END HelloWorld1! PL/ Hello Tom; 8 / 7 /);Tom'Hello '. 执行 SQL> exec CallHelloWorld1( ' set serveroutput on SQL> 7 END CallHelloWorld1; CREATE OR REPLACE PROCEDURE HelloW ...
  • 你有没有尝试过以下方法: 在想要查看该值的行上放置一个断点。 在该文件上运行调试器并切换到“变量”选项卡( Window > Debugger > Variables )。 这将在该断点处显示变量的值。 这些行也可能有子行 - 例如。 如果有一个名为myArray的数组,您可以单击它旁边的+符号来查看每个元素值。 您还可以通过执行'Debug' > 'Evaluate Expression'来评估条件。 例如,在'myArray'的迭代循环中,可以输入myArray[2] == 5 ,然后单击绿色->箭头来 ...
  • 这一变化是为了应对CWG缺陷633 。 与此相关的更改列表可在以下网址找到: 这些变化的目标是扩大“变量”的含义以包含命名对象和引用,并在可行的情况下始终如一地应用该术语。 The change was in response to CWG defect 633. The list of changes related to this can be found in n2993: The goal of these changes is to expand the meaning of "variable" ...
  • 我相信你要找的是用户定义的表类型。 这将允许您使用任意数量的列传递任意数量的可能答案。 create type dbo.possible_answer as table( answer nvarchar(max), explaination nvarchar(max), review nvarchar(max) ); create procedure dbo.create_question @QuestionType int, ....(Other params here) @Possi ...
  • 函数会反复调用自身,直到满足某些条件。 一个愚蠢的例子:如何递归地走100步: function walk(steps) { if (steps > 0) { // not there yet take 1 step walk(steps - 1); // we're 1 step closer, now walk the rest of the way } else { // we're there already, don't walk any fu ...
  • 这是一个例子。 基本上,RETURNING子句列出了类型(可选地后跟AS名称 - 但名称不声明变量); 然后在程序的主体中,您可以像往常一样定义变量,然后返回它们。 这个特殊的例子是对Oracle的NEXT_DAY函数的模拟,减去了全球化。 该文件包括自检: 示例程序 -- @(#)$Id: next_day.spl,v 1.1 2004/10/05 21:39:18 jleffler Exp $ -- -- @(#)Create procedure equivalent to Oracle's NEXT_ ...
  • 对于从存储过程执行的代码的特定情况,隔离级别将重置为调用存储过程之前的状态,无论发生什么情况。 来自文档 : 如果在存储过程或触发器中发出SET TRANSACTION ISOLATION LEVEL ,则当对象返回控件时,隔离级别将重置为调用对象时生效的级别。 如果您没有使用存储过程,或者您正在处理存储过程中的代码,那么(更复杂的)故事如下所示。 隔离级别是会话的属性,而不是数据库的属性。 是否在错误发生后执行SET TRANSACTION ISOLATION LEVEL类的语句取决于错误是中止整个批处理 ...