数据库动态生成树

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

数据库动态生成树importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.Statement;importjavax.swing.JFrame;importjavax.swing.JTree;importjavax.swing.tree.DefaultMutableTreeNode;publicclassMainextendsJFrame{Main(){DefaultMutableTreeNodenode=newDefaultMutableTreeNode(地板砖);createNodes(node,0);JTreetree=newJTree(node);add(tree);setSize(200,200);setVisible(true);}publicstaticvoidmain(String[]argv)throwsException{newMain();}publicvoidcreateNodes(DefaultMutableTreeNodeparent,StringParentID){DefaultMutableTreeNodetemp=null;Stringstrsql=selectID,Name,ParentIDfromwareswhereParentID='+ParentID+';try{Connectioncon;con=DriverManager.getConnection(jdbc:sqlserver://localhost:1433;DatabaseName=MyShop,root,root);//建立Statement对象Statementste=con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);//执行数据库查询语句ResultSetrs=ste.executeQuery(strsql);while(rs.next()){temp=newDefaultMutableTreeNode(rs.getString(Name));parent.add(temp);createNodes(temp,rs.getString(ID));//递归遍历System.out.println(rs.getString(name));}rs.close();ste.close();con.close();}catch(Exceptione){System.out.println(e);}}}

1 / 2
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功