您好,欢迎访问三七文档
数据库动态生成树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);}}}
本文标题:数据库动态生成树
链接地址:https://www.777doc.com/doc-2332595 .html