生活随笔
收集整理的這篇文章主要介紹了
递归1
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ?此次做項目使用JDBC,遇到了一個樹形結構,采用遞歸查找父類,有了一個初步的認識。
?
public?Channel?findParent(int?id){?????????if?(id==0)?{?????????????return?null;?????????}else?{?????????int?parent?=?1;?????????????Channel?c=new?Channel();?????????????String?sql="select?*?from?channel?where?id=?";?????????????conn=DBConnection.getConnection();?????????????try?{?????????????????ps=conn.prepareStatement(sql);?????????????????ps.setInt(1,?id);?????????????????rs=ps.executeQuery();?????????????????while?(rs.next())?{?????parent=rs.getInt("parent");?????????c.setChannelName(rs.getString("channelName"));??????????????????}?????????????????rs.close();?????????????????ps.close();?????????????????conn.close();?????????????????if?(parent!=0)?{???- 1.????????????c.setChaparent(findParent(parent));?
????????????????}?????????????}?catch?(SQLException?e)?{?????????????????e.printStackTrace();?????????????}??????????????return?c;??????????}??????}?先說遞歸的執行順序:類似于攔截器,if (parent!=0)則一直執行1.以前的語句 else 執行1.以后的語句。并且執行順序是1 2 3 。3 2 1
轉載于:https://blog.51cto.com/4215825/1107493
總結
以上是生活随笔為你收集整理的递归1的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。