📄 managerdao.java
字号:
this.cList.add(listParent.get(i));// 块节点放入泛型
this.getAllLeafById(sParentId);// 获得当前根节点下的所有子节点
}
return this.cList;
}
/**
* 根据版块id,递归获得当前根节点下的所有子节点
*
* @param sId
* 版块id
* @param flag
* 是否最后一个根节点
* @param isLastNode
* 是否最后一个节点 false:未知的 true:确定的 注意:在不确定的情况下请使用false
*/
private void getAllLeafById(Integer sId, Boolean flag) {
// 带前导图片的
List<SectionInfo> list = this.getSectionById(sId);// 获得当前节点下全部子节点
String image = "";
String signStr = "";
String name = "";
if (list.size() == 0) {
return;
}
for (int i = 0; i < list.size(); i++) {
sId = list.get(i).getSid();
/* 带前导图片的树形版块格式化输出 主要实现四步曲 */
this.sign.clear(); // 初始化
signStr = ""; // 初始化
// 1、获得分割符
traverseRootNodeById(list.get(i).getSparentid());// 递归获得当前节点的所有上层节点,直到根节点为止,从而格式化分割符号
if (i == list.size() - 1) {// 如果是最后一个节点时,设置开关
image = lastLeafImage;
} else {
image = leafImage;
}
// 2、格式化分割符
if (flag && this.sign.size() != 0) {// 当遍历到最后一个根节点的时候,在版块名的最前面改为一个空格符
this.sign.set(this.sign.size() - 1, noexpandImage);// 把数组sign最后一个元素改为空格
}
for (int j = this.sign.size(); j > 0; j--) {// 倒序遍历sign符号数组
signStr += this.sign.get(j - 1);
}
// 3、把结果存入泛型
name = "<a href=\"../servletListPage?sid=" + list.get(i).getSid()
+ "\" target=\"_blank\"> " + list.get(i).getSname()
+ "</a>";
allList
.add("<div onMouseOver=\"this.style.backgroundColor=\'#7DDFFF\'\" onMouseOut=\"this.style.backgroundColor=\'#FFFFFF\'\">"
+ signStr
+ image
+ name
+ " "
+ arrow
+ addBt
+ sEditBt
+ list.get(i).getSid()
+ eEditBt
+ sDelBt
+ list.get(i).getSid()
+ eDelBt
+ sMoveBt
+ list.get(i).getSid()
+ eMoveBt
+ "</div>");// 版块节点放入泛型
System.out.println(signStr + "[" + list.get(i).getSname() + "]");
// 4、递归
getAllLeafById(sId, flag);// 再次调用自己(递归)
}
}
/**
* 根据版块id,递归获得当前根节点下的所有子节点
*
* @param sId
* 版块id
*/
private void getAllLeafById(Integer sId) {
// 带前导 的
List<SectionInfo> list = this.getSectionById(sId);// 获得当前节点下全部子节点
if (list.size() == 0) {
return;
}
for (int i = 0; i < list.size(); i++) {
sId = list.get(i).getSid();
cList.add(list.get(i));// 版块节点放入泛型
getAllLeafById(sId);// 再次调用自己(递归)
}
}
/**
* 添加版块
*
* @param sParentId
* 父版块编号
* @param sName
* 版块名
* @return Boolean 返回一个布尔型
*/
public Boolean addSection(Integer sParentId, String sName) {
Integer result = -1;
String sql = "insert into sectionInfo(sName,sParentId) values(?,?)";
try {
result = dao.executeUpdate(sql, new Object[] { sName, sParentId });
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeResultSet();
dao.closeStatement();
dao.closeConnection();
}
return result > 0 ? true : false;
}
/**
* 根据版块id获得版块信息
*
* @param sId
* 版块编号
* @return SectionInfo 返回一个对象
*/
public SectionInfo getSectionNameById(Integer sId) {
String sql = "select * from sectionInfo where sid = ?";
SectionInfo obj = null;
try {
rs = dao.executeQuery(sql, new Object[] { sId });
if (rs != null && rs.next()) {
obj = new SectionInfo();
obj.setSid(rs.getInt("sId"));
obj.setSmasterid(rs.getInt("sMasterId"));
obj.setSname(rs.getString("sName"));
obj.setSparentid(rs.getInt("sParentId"));
obj.setStopiccount(rs.getInt("sTopicCount"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeResultSet();
dao.closeStatement();
dao.closeConnection();
}
return obj;
}
/**
* 修改版块信息
*
* @param sId
* 版块编号
* @param sName
* 版块名
* @return Boolean 返回一个布尔型
*/
public Boolean updateSectionInfoById(Integer sId, String sName) {
String sql = "update sectionInfo set sName = ? where sId = ?";
Integer result = 0;
try {
result = dao.executeUpdate(sql, new Object[] { sName, sId });
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeResultSet();
dao.closeStatement();
dao.closeConnection();
}
return result > 0 ? true : false;
}
/**
* 把源版块作为目标版块的子版块
*
* @param sourceSid
* 原版块编号
* @param targetSid
* 目标版块编号
* @return Boolean 返回一个布尔型 true:成功
*/
public Boolean moveToChildSectionInfoById(Integer sourceSid,
Integer targetSid) {
if (sourceSid == targetSid) {// 自身不能移动
return false;
}
String sql = "update sectionInfo set sParentId = ? where sid = ?";
Integer result = 0;
try {
result = dao.executeUpdate(sql,
new Object[] { targetSid, sourceSid });
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeResultSet();
dao.closeStatement();
dao.closeConnection();
}
return result > 0 ? true : false;
}
/**
* 把源版块作为根版块
*
* @param sourceSid
* 原版块编号
* @return Boolean 返回一个布尔型 true:成功
*/
public Boolean moveToRootSectionInfoById(Integer sourceSid) {
String sql = "update sectionInfo set sParentId = 0 where sid = ?";
Integer result = 0;
try {
result = dao.executeUpdate(sql, new Object[] { sourceSid });
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeResultSet();
dao.closeStatement();
dao.closeConnection();
}
return result > 0 ? true : false;
}
/**
* 根据版块id,判断是否含有子节点
*
* @param sid
* 版块编号
* @return Boolean 返回一个布尔型 true:是
*/
public Boolean isHaveChildNode(Integer sid) {
String sql = "select * from sectionInfo where sParentId = ?";
try {
rs = dao.executeQuery(sql, new Object[] { sid });
if (rs != null && rs.next()) {
return true;
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeResultSet();
dao.closeStatement();
dao.closeConnection();
}
return false;
}
/**
* 根据版块id,删除所有主帖和跟帖
*
* @param sid
* 版块编号
* @return Boolean 返回一个布尔型 true:成功
*/
public Boolean delSectionInfo(Integer sid) {
String sql1 = "delete from replyInfo where rSid = ?";// 先删除跟帖表
String sql2 = "delete from topicInfo where tSid = ?";// 再删除主帖表
String sql3 = "delete from sectionInfo where sid = ?";// 最后删除版块表
Integer result = 0;
try {
result = dao.executeUpdate(sql1, new Object[] { sid });
result = dao.executeUpdate(sql2, new Object[] { sid });
result = dao.executeUpdate(sql3, new Object[] { sid });
} catch (SQLException e) {
e.printStackTrace();
} finally {
dao.closeResultSet();
dao.closeStatement();
dao.closeConnection();
}
return result > 0 ? true : false;
}
public static void main(String[] args) {
// 变量声明
SectionInfoDAO section_dao = new SectionInfoDAO();
ManagerDAO manager_dao = new ManagerDAO();
List<SectionInfo> clist = null;
String blank = "";
//
List<SectionInfo> listParent = section_dao.getSectionById(0);
clist = manager_dao.getAllSectionObjByList(listParent);
// 先获得所有节点对象
for (int i = 0; i < clist.size(); i++) {
// 根据对象的spid 获得版块名
blank = manager_dao.traverseRootNodeById(clist.get(i)
.getSparentid(), "&bnsp;");
System.out.println(blank + clist.get(i).getSname());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -