|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
市场分额,java比asp高一点,因为C#是仿照java开发的,所以哦C#能做的java都能做到,但是java能做的,C#不一定都能做到。毕竟是抄袭吗。js|递回|数据|数据库树形目次的递回完成(一)数据库+jsp+javabean
<%@pagecontentType="text/html;charset=gb2312"language="java"import="java.sql.*"errorPage=""%>
<!--
数据库布局:
库名:test
表名:tree
CREATETABLE[dbo].[tree](
[id][int]IDENTITY(1,1)NOTNULL,
[parentid][int]NOTNULL,
[message][varchar](50)COLLATEChinese_PRC_CI_ASNULL
)
为了到达对照好的效果,这里筹办了五张小图片
加号:http://www.webjx.com/htmldata/2005-09-27/plus.gif
减号:http://www.webjx.com/htmldata/2005-09-27/minus.gif
翻开的文件夹:http://www.webjx.com/htmldata/2005-09-27/openfold.gif
封闭的文件夹:http://www.webjx.com/htmldata/2005-09-27/closedfold.gif
白板:http://www.webjx.com/htmldata/2005-09-27/white.gif
-->
<%!//便利起见这里就不写成javabean了
classcn{//毗连数据库,这里以MS-SQL为例
StringjdbcDriver="com.microsoft.jdbc.sqlserver.SQLServerDriver";//jdbc驱动
StringconnectionString="jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=test";//数据库毗连字符串
Stringuser="sa";//数据库用户名
Stringpass="";//数据库暗码
Connectionconn=null;
ResultSetrs=null;
publiccn(){
try{
Class.forName(jdbcDriver);
}catch(ClassNotFoundExceptione){
System.err.println(e.toString());
}
}
publicResultSetexecuteQuery(Stringsql){
rs=null;
try{
conn=DriverManager.getConnection(connectionString,user,pass);
Statementstmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
rs=stmt.executeQuery(sql);
}
catch(SQLExceptione){
System.err.println(e.toString());
}
returnrs;
}
publicvoidexecuteUpdate(Stringsql){
try{
conn=DriverManager.getConnection(connectionString,user,pass);
Statementstmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
stmt.executeUpdate(sql);
}
catch(SQLExceptione){
System.err.println(e.toString());
}
}
}
classtree{
cnconn=newcn();
publicvoidinit(javax.servlet.jsp.JspWriterout,javax.servlet.http.HttpServletRequestrequest)throwsException{
out.println("<title>用jsp种树</title>");
dowith(request);
buildTree(out,0,0);//初始挪用
}
privatevoiddowith(javax.servlet.http.HttpServletRequestrequest){
if(request.getParameter("parentid")==null||request.getParameter("parentid").equals(""))return;
Stringaction=request.getParameter("action");
if(action.equals("add"))
conn.executeUpdate("insertintotree(parentid,message)values("+request.getParameter("parentid")+","+request.getParameter("message")+")");
elseif(action.equals("delete"))
conn.executeUpdate("deletefromtreewhereid="+request.getParameter("parentid")+"orparentid="+request.getParameter("parentid"));
}
publicvoidbuildTree(javax.servlet.jsp.JspWriterout,intparentid,intlevel)throwsException{
level++;
ResultSetrs=conn.executeQuery("select*fromtreewhereparentid="+parentid+"orderbyid");
while(rs.next()){
out.println("<div>");
for(inti=0;i<level-1;i++)
out.print("");
out.print("<span+rs.getInt("id")+");"style="cursor:default;"id="span"+rs.getInt("id")+"">"+rs.getString("message")+"id="+rs.getInt("id")+"</span>");
out.println("<divstyle="display:none;"id="div"+rs.getInt("id")+"">");
buildTree(out,rs.getInt("id"),level);//递回挪用
out.println("</div>");
}else
out.print("<span+rs.getInt("id")+");"style="cursor:default;"id="span"+rs.getInt("id")+"">"+rs.getString("message")+"id="+rs.getInt("id")+"</span>");
out.println("</div>");
}
rs.close();
rs=null;
}
privatebooleanhas_child(intparentid)throwsException{
ResultSetrs=conn.executeQuery("select*fromtreewhereparentid="+parentid+"orderbyid");
returnrs.next();
}
publicStringgetOption()throwsException{
Stringoption="";
ResultSetrs=conn.executeQuery("select*fromtreeorderbyid");
while(rs.next())
option+="<optionvalue=""+rs.getInt("id")+"">"+rs.getInt("id")+"</option>";
returnoption;
}
}
%>
<!--以上代码能够写成javabean-->
<scriptlanguage="JavaScript"><!--这段js为了完成树的睁开和封闭的效果-->
<!--
functionmyClick(id){
eval("vardiv=div"+id);
eval("varimg=img"+id);
eval("varim=im"+id);
div.style.display=div.style.display!="none"?"none":"block";
img.src=div.style.display!="none"?"http://www.webjx.com/htmldata/2005-09-27/minus.gif":"http://www.webjx.com/htmldata/2005-09-27/plus.gif";
im.src=div.style.display!="none"?"http://www.webjx.com/htmldata/2005-09-27/openfold.gif":"http://www.webjx.com/htmldata/2005-09-27/closedfold.gif";
img.alt=div.style.display!="none"?"封闭":"睁开";
}
functionmyClick1(id){
document.form1.parentid.value=id;
}
//-->
</script>
<table>
<tr><tdheight="300"valign="top">
<%
treemyTree=newtree();
myTree.init(out,request);
%>
</td></tr>
<tr><tdvalign="top">
<%
if(myTree.getOption().equals("")){
%>
<formname="form1"action=""method="get">
parentid:0-暗示根节点
message:<inputtype="text"name="message">
<inputtype="hidden"name="parentid"value="0">
<inputtype="submit"name="action"value="add">
</form>
<%}else{%>
<formname="form1"action=""method="get">
parentid:<selectname="parentid"><%=myTree.getOption()%></select>
message:<inputtype="text"name="message">
<inputtype="submit"name="action"value="add"><inputtype="submit"name="action"value="delete">
</form>
<%}%>
</td></tr></table>
再说第三点:我并没有提到服务器也要整合,然后是IDE,一个好的IDE能够200%提高开发的速度,就说图形方面:你是经过简单托拽和点击就能实现功能好那。 |
|