📄 treedatapro.java
字号:
package com.shyhao.qq.service;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import net.sf.json.JSONArray;
import com.shyhao.qq.jobo.TreeNode;
public class TreeDataPro {
private String ParentId;
//数据库连接参数
private String DRIVER = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
private String URL = "jdbc:sqlserver://localhost:1433; DatabaseName=shyhao";
private String NAME = "songyinghao";
private String PASSWORD = "159";
public TreeDataPro() {
try {
Class.forName(DRIVER);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public String getParentId() {
return ParentId;
}
public void setParentId(String parentId) {
ParentId = parentId;
}
/**
* 方法: getJsonString()
* 作用:从数据库中提取所需的节点信息
* 并转化成json类型
* 返回
*/
public String getJsonString() {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
ArrayList<TreeNode> TreeNodeArray = null;
String SQLString = "select * from QQUser where ParentId="
+ this.ParentId +"and OnLine='1'"+ " order by NodeId";
try {
conn = DriverManager.getConnection(URL, NAME, PASSWORD);
stmt = conn.createStatement();
rs = stmt
.executeQuery("select ParentId from QQUser where ParentId>0 group by ParentId order by ParentId");
StringBuffer parentIdBuffer = new StringBuffer();
parentIdBuffer.append("|");
// 得到所有的parentDID
while (rs.next()) {
parentIdBuffer.append(rs.getString("ParentId"));
parentIdBuffer.append("|");
}
// 转化为字符串
String parentIdString = parentIdBuffer.toString();
//查询父节点为ParentId时所包含的信息
rs = stmt.executeQuery(SQLString);
TreeNodeArray = new ArrayList<TreeNode>();
while (rs.next()) {
TreeNode TreeNode = new TreeNode();
TreeNode.setId(rs.getString("NodeId"));
TreeNode.setText(rs.getString("UserName"));
TreeNode.setDescription(rs.getString("UserName"));
if (parentIdString.indexOf("|" + rs.getString("NodeId") + "|") >= 0) {
//父节点
TreeNode.setCls("folder");
TreeNode.setLeaf(false);
TreeNode.setExpandable(false);
} else {
//子节点
TreeNode.setCls("file");
TreeNode.setLeaf(true);
TreeNode.setExpandable(false);
}
TreeNodeArray.add(TreeNode);
}
// 得到JSON数组
JSONArray jsonArr = JSONArray.fromObject(TreeNodeArray);
// 把JSON数组转化为String类型并返回
return jsonArr.toString();
} catch (Exception e) {
e.printStackTrace();
System.out.println("TreeDataPro.java is in "+e);
return "";
} finally {
try {
//关闭连接
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -