问题描述
news_types{ id parentid message}最终输入结果显示出级别 问题补充:chen_yongkai 写道
解决方案
写了这么多才3分。。。哎,真是不容易啊
解决方案二:
这个是基于广度遍历优先的算法,News_type 为一个树下面是怎么构造出树和怎么去遍历树public class DG{ public static void main(String args[]) throws SQLException { News_type n = getNewsTreeById("8a65adf12e504bf5012e5050c72c0008"); show(n); } public static void show(News_type n){ if(n.getSons().size()==0){ System.out.println(n.getId()); return ; }else{ for(News_type n1 :n.getSons()) show(n1); } } public static News_type getNewsTreeById(String id) throws SQLException{ Connection conn = DBhelper.getConnection(); PreparedStatement st = null; ResultSet rs = null; News_type news_tree=new News_type(); news_tree.setSons(new HashSet<News_type>()); try { st = conn.prepareStatement("select * from TB_SYS_RESOURCES where parentid = ?"); st.setString(1, id); rs = st.executeQuery(); while(rs.next()){ News_type news = new News_type(rs.getString(1),new HashSet<News_type>() ,rs.getString(3)); news_tree.getSons().add(news); } rs.close(); getTree(st, news_tree); } catch (Exception e) { }finally{ conn.close(); } return news_tree; } private static void getTree(PreparedStatement st, News_type news_tree) throws SQLException { ResultSet rs; for(News_type n :news_tree.getSons()){ st.setString(1, n.getId()); rs = st.executeQuery(); while(rs.next()){ News_type news = new News_type(rs.getString(1),new HashSet<News_type>() ,rs.getString(3)); n.getSons().add(news); } rs.close(); } for(News_type n :news_tree.getSons()){ getTree(st, n); } } }class News_type{ public News_type(){} public News_type(String id, Set<News_type> sons, String message) { super(); this.id = id; this.sons = sons; this.message = message; } private String id; private Set<News_type> sons; private String message; public String getId() { return id; } public void setId(String id) { this.id = id; } public Set<News_type> getSons() { return sons; } public void setSons(Set<News_type> sons) { this.sons = sons; } public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } @Override public int hashCode(){ return this.id.hashCode(); } @Override public boolean equals(Object obj) { if(obj instanceof News_type){ News_type cast = (News_type)obj; if(cast.getId().equals(cast.getId())) return true; } return false; }}
解决方案三:
我明白你的意思了,自己写的话,通过id抓取一整个树的话可以是可以,而且比较麻烦,没有延迟加载这个很耗费内存,建议使用hibernate,通过它的级联关系来获取整个树,不用写什么代码,做些配置文件就可以了
解决方案四:
根据id来查询 节点下面的所有id么,还有要求是什么格式的?
解决方案五:
ok 上java代码这个Connection我没有写,你就按照你们项目的来 public int getLevelById(String id) throws SQLException{ Connection conn = DriverManager.getConnection(""); PreparedStatement st = null; ResultSet rs = null; int level = 1; try { String parentid = null; st = conn.prepareStatement("select parentid from news_types where id = ?"); st.setString(1,id); do{ rs = st.executeQuery(); if(rs.next()){ parentid = rs.getString(1); level++; rs.close(); } st.setString(1, parentid); }while(parentid!=null); } catch (Exception e) { }finally{ conn.close(); } return level; }
解决方案六:
楼主你明明写的是java嘛。。。sql什么的存储过程这个写的很清楚[url]http://blog.csdn.net/ACMAIN_CHM/article/details/4142971[/url]
解决方案七:
这个结构明显就不好使嘛一般是这个结构class Strut{private String id;private Strut parent;private String message;//get...//set...}public int getLevel(Strut s){ int level = 0; while((s=s.getParent())!=null){ level++; } return level;}
解决方案八:
oracle中这么玩select id ,parentid,message from news_types start with id = 1 connect by prior id = parentid 可以在表名后面加过滤条件。
解决方案九:
神马情况?问题描述好简约!引用最终输入结果 显示出级别