代码如下:
package 算法and数据结构.数据结构.多叉树.文件管理系统.从数据库中拿取数据并建树;
import 算法and数据结构.数据结构.多叉树.文件管理系统.Node.Node;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import static 算法and数据结构.数据结构.多叉树.文件管理系统.连接数据库.getMySQL.getConnection;
public class creat {
String select = "select file_name from file_system where parent_file_name = '{' and user_name = '{'";
public creat(Node root){
// select(root,"893295815@qq.com");
select(root,"NCEPU_RuanCong");
System.out.println("=======================前======================");
text.pre(root);
System.out.println("=======================中======================");
text.mid(root);
System.out.println("=======================后======================");
text.pos(root);
}
private String getSelectSQL(String ...args) {
StringBuilder stringBuilder = new StringBuilder(args[0]);
int j = 1;
for (int i = 0; i < stringBuilder.length(); i++) {
if (stringBuilder.charAt(i) == '{'){
stringBuilder.replace(i,i+1,args[j]);
i=i+args[1].length();
j++;
}
}
return stringBuilder.toString();
}
private void select(Node root,String username){
if (root == null)return;
Connection connection=getConnection();
Statement statement=null;
ResultSet resultSet=null;
try {
statement=getConnection().createStatement();
System.out.println(getSelectSQL(select,root.date,username));
resultSet=statement.executeQuery(getSelectSQL(select,root.date,username));
while (resultSet.next()){
root.children.add(new Node(resultSet.getString("file_name"),root,1));
}
for (int i = 0; i < root.children.size(); i++) {
select(root.children.get(i),username);
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
if (connection != null){
try {
connection.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (statement != null){
try {
statement.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
if (resultSet != null) {
try {
resultSet.close();
} catch (SQLException throwables) {
throwables.printStackTrace();
}
}
}
}
}
class text{
public static void main(String[] args) {
// new creat(new Node("Is 893295815@qq.com Computer",null,1));
new creat(new Node("Is NCEPU_RuanCong Computer",null,1));
}
public static void pre(){
}
public static void pre(Node root){
if(root == null)return;
System.out.println(root.date);
for(int i=0 ; i< root.children.size() ;i++){
pre(root.children.get(i));
}
}
public static void mid(Node root){
if(root == null)return;
if (root.children.size() != 0){
mid(root.children.get(0));
}
System.out.println(root.date);
for(int i = 1 ; i < root.children.size() ; i++){
mid(root.children.get(i));
}
}
public static void pos(Node root){
if (root == null){
return;
}
for (int i = 0; i < root.children.size(); i++) {
pos(root.children.get(i));
}
System.out.println(root.date);
}
}
以下是代码运行的结果: