0
回答
easy ui tree 递归计算问题
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   
package web;


import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;


import net.sf.json.JSONArray;
import util.ConnectionManager;


/**
 * 查询数据使用JDBC进行操作
 *
 */
public class TreeAction {
private List<TreeNode> treeNodes = new ArrayList<TreeNode>(); // 返回的JSON数据
private String id; // 树组件使用的ID


public String treeLoad() throws IOException {


Statement sta = null;
ResultSet rs = null;
try {
Connection conn = ConnectionManager.getConnection();
sta = conn.createStatement();
String sql = "";
if (id == null) {
sql = "select * from easyui_tree where parentid = ''";
} else {
sql = "select * from easyui_tree where parentid = " + id;
}
rs = sta.executeQuery(sql);


while (rs.next()) {
String id1 = rs.getString("id");
String name = rs.getString("name");
String parentid = rs.getString("parentid");
TreeNode node = new TreeNode();
node.setId(id1);
node.setText(name);
node.setChecked(false);
node.setChildren(find(id1));


if (isChildrenNode(id1)) {
node.setState("closed");
} else {
node.setState("open");
}


treeNodes.add(node);


}


ConnectionManager.closeAll(rs, sta, conn);
} catch (SQLException e) {
e.printStackTrace();
}


return "success";
}


public List<TreeNode> find(String icd) {
Statement sta = null;
ResultSet rs = null;
try {
Connection conn = ConnectionManager.getConnection();
sta = conn.createStatement();
String sql = "";
if (icd == null) {
return null;
} else {
sql = "select * from easyui_tree where parentid = " + icd;
}
rs = sta.executeQuery(sql);


while (rs.next()) {
String id = rs.getString("id");
String name = rs.getString("name");
TreeNode node = new TreeNode();
node.setId(id);
node.setText(name);
node.setChecked(false);
// node.setChildren(find(id));
if (isChildrenNode(id)) {
node.setState("closed");
} else {
node.setState("open");
}


treeNodes.add(node);


}


ConnectionManager.closeAll(rs, sta, conn);
} catch (SQLException e) {
e.printStackTrace();
}


return treeNodes;


}


public boolean isChildrenNode(String id) {
Boolean flag = false;
Statement sta = null;
ResultSet rs = null;
try {
Connection conn = ConnectionManager.getConnection();
sta = conn.createStatement();
String sql = "select * from easyui_tree where parentid = " + id;
rs = sta.executeQuery(sql);
while (rs.next()) {
flag = true;
}


ConnectionManager.closeAll(rs, sta, conn);
} catch (SQLException e) {
e.printStackTrace();
}
return flag;
}


public List<TreeNode> getTreeNodes() {
return treeNodes;
}


public void setTreeNodes(List<TreeNode> treeNodes) {
this.treeNodes = treeNodes;
}


public String getId() {
return id;
}


public void setId(String id) {
this.id = id;
}


}

请高手帮我看看 我的方法哪里有问题  正确的应该是



举报
顶部