很久以前的一棵树

系统功能表:inside_tree

id

int

4

 

parentid

int

4

 

message

varchar

50

 

说明

varchar

50

Null

 

 

<textarea cols="50" rows="15" name="code" class="c-sharp">&lt;%@page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %&gt; &lt;%@page import="java.util.*"%&gt; &lt;%!//方便起见这里就不写成javabean了 class cn {//连接数据库,这里以MS-SQL为例 String jdbcDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";//jdbc驱动 String connectionString="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=sunmoon";//数据库连接字符串 String user="sa";//数据库用户名 String pass="sa";//数据库密码 Connection conn=null; ResultSet rs=null; public cn() { try { Class.forName(jdbcDriver); } catch(ClassNotFoundException e) { System.err.println(e.toString()); } } public ResultSet executeQuery(String sql) { rs=null; try { conn=DriverManager.getConnection(connectionString,user,pass); Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); rs=stmt.executeQuery(sql); } catch(SQLException e) { System.err.println(e.toString()); } return rs; } public void executeUpdate(String sql) { try { conn=DriverManager.getConnection(connectionString,user,pass); Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE); stmt.executeUpdate(sql); } catch(SQLException e) { System.err.println(e.toString()); } } } class tree { cn conn=new cn(); public void init(javax.servlet.jsp.JspWriter out,javax.servlet.http.HttpServletRequest request, String username) throws Exception { out.println("&lt;title&gt;系统管理员功能&lt;/title&gt;"); buildTree(out,0,0,username);//初始调用 } public void buildTree(javax.servlet.jsp.JspWriter out,int parentid,int level,String username) throws Exception { level++; ResultSet rs=conn.executeQuery("select * from admin_tree where parentid="+parentid+" order by id"); // ResultSet rs2=conn.executeQuery("select * from parentRights where username='sensory' order by parentId"); //select * from tree where parentid in (select parentId from parentRights )order by id while(rs.next()) { int id; id=rs.getInt("id"); String idStr = String.valueOf(id); //int转化为String //String userName="a"; //替换为session名 ResultSet rs2=conn.executeQuery("select * from admin_function where username='"+username+"' and parentid='"+idStr+"' order by parentId"); if (!rs2.next()) //if((rs.getInt("id")!=6) &amp;&amp; (rs.getInt("id")!=10)) //权限管理 { out.println("&lt;div&gt;"); for(int i=0;i&lt;level-1;i++) out.print("&lt;img src="/" mce_src="/""white.gif/"&gt; "); if(has_child(rs.getInt("id"))) { out.print("&lt;img alt=/"展开/" style="/" mce_style="/""cursor:hand;/" onclick=/"myClick('"+rs.getInt("id")+"');/" id=/"img"+rs.getInt("id")+"/" src="/" mce_src="/""plus.gif/"&gt; &lt;img id=/"im"+rs.getInt("id")+"/" src="/" mce_src="/""closedfold.gif/"&gt; "); out.print("&lt;span onclick=/"myClick1('"+rs.getInt("id")+"');/" style="/" mce_style="/""cursor:default;/" id=/"span"+rs.getInt("id")+"/"&gt;"+rs.getString("message")+" &lt;/span&gt;"); out.println("&lt;div style="/" mce_style="/""display:none;/" id=/"div"+rs.getInt("id")+"/"&gt;"); buildTree(out,rs.getInt("id"),level,username);//递归调用 out.println("&lt;/div&gt;"); } else out.print("&lt;img src="/" mce_src="/""dot.gif/"&gt;&lt;span onclick=/"myClick1('"+rs.getInt("id")+"');/" style="/" mce_style="/""cursor:default;/" id=/"span"+rs.getInt("id")+"/"&gt;"+rs.getString("message")+" &lt;/span&gt;"); out.println("&lt;/div&gt;"); } } rs.close(); rs=null; } private boolean has_child(int parentid) throws Exception { ResultSet rs=conn.executeQuery("select * from admin_tree where parentid="+parentid+" order by id"); return rs.next(); } } %&gt; &lt;!--以上代码可以写成javabean--&gt; &lt;mce:script language="JavaScript"&gt;&lt;!-- 这段js为了实现树的展开和关闭的效果--&gt; &lt;!-- function myClick(id) { eval("var div=div"+id); eval("var img=img"+id); eval("var im=im"+id); div.style.display=div.style.display!="none"?"none":"block"; img.src=div.style.display!="none"?"minus.gif":"plus.gif"; im.src=div.style.display!="none"?"openfold.gif":"closedfold.gif"; img.alt=div.style.display!="none"?"关闭":"展开"; } function myClick1(id) { //document.form1.parentid.value=id; admin/sysManage.jsp if (id==101) { window.open('../admin/userManage/userRun.jsp','message','scrollbars=auto,status=yes,toolbar=yes,menubar=yes,location'); } if (id==102) { window.open('../admin/userManage/userFunction.jsp','message','scrollbars=auto,status=yes,toolbar=yes,menubar=yes,location'); } if (id==1001) { window.open('../admin/vote/votemain.jsp','message','scrollbars=auto,status=yes,toolbar=yes,menubar=yes,location'); } if (id==2001) { window.open('../admin/notebook/adminnotedetail.jsp','message','scrollbars=auto,status=yes,toolbar=yes,menubar=yes,location'); } if (id==5001) { window.open('../message/adminmsgcenter.jsp','message','scrollbars=auto,status=yes,toolbar=yes,menubar=yes,location'); } if (id==8001) { window.open('../admin/database/backup.jsp','message','scrollbars=auto,status=yes,toolbar=yes,menubar=yes,location'); } if (id==8002) { window.open('../admin/database/restore.jsp','message','scrollbars=auto,status=yes,toolbar=yes,menubar=yes,location'); } else { } } // --&gt;&lt;/mce:script&gt; &lt;html&gt; &lt;table&gt; &lt;tr&gt; &lt;td height="300" valign="top"&gt; &lt;% String username = (String) session.getValue("userid"); tree myTree=new tree(); myTree.init(out,request,username); %&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt;&lt;strong&gt;&lt;font size="1"&gt;&lt;%=username%&gt;&lt;/font&gt;&lt;/strong&gt; &lt;/tr&gt;&lt;/table&gt; &lt;/html&gt;</textarea>

你可能感兴趣的:(exception,数据库,String,tree,sqlserver,scroll)