产品目录-论坛 树形结构的实现.htm
来自「写给JSP初级程序员的书」· HTM 代码 · 共 37 行
HTM
37 行
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0057)http://eps.www85.cn4e.com/java/article/devshow.asp?id=183 -->
<HTML><HEAD><title>csdn_产品目录/论坛 树形结构的实现</TITLE>
<META content="text/html; charset=gb2312" http-equiv=Content-Type>
<STYLE type=text/css>TD {
FONT-FAMILY: "Verdana", "Arial", "宋体"; FONT-SIZE: 9pt
}
A {
COLOR: #660000; TEXT-DECORATION: underline
}
A:hover {
COLOR: #660000; TEXT-DECORATION: none
}
.line {
LINE-HEIGHT: 14pt
}
</STYLE>
<META content="MSHTML 5.00.2920.0" name=GENERATOR></HEAD>
<BODY bgColor=#ffffff text=#000000>
<table><tbody>
<TR>
<TD height=21>
<DIV align=center><B><FONT size=3>产品目录/论坛 树形结构的实现 <BR><FONT
size=2>
</FONT></FONT></FONT>
<HR align=center color=#cccccc noShade SIZE=1>
</DIV></TD></TR>
<TR>
<TD class=line><FONT
color=#333300>产品目录/论坛 树形结构的实现 <BR><BR>发布日期:2001/09/20 发 布 者:51jsp.net <BR> <BR> 原理请参见www.chinaasp.com/sqlbbs中的ASP论坛版精华区 <BR>本例采用Access做为实例,所有还有很多要改进的地方。 <BR>请读者自已改进 <BR><BR>package product; <BR>import java.sql.*; <BR>public class ConnectDB{ <BR>String strDBDriver="sun.jdbc.odbc.JdbcOdbcDriver"; <BR>String strConnstr="jdbc:odbc:product"; <BR>Connection conn=null; <BR>ResultSet rs=null; <BR>Statement stmt=null; <BR>// Statement stmt=null; <BR>//构造函数 <BR>public ConnectDB(){ <BR>try{ <BR>Class.forName(strDBDriver); conn=DriverManager.getConnection(strConnstr); <BR>stmt=conn.createStatement(); <BR>} <BR>catch(Exception e){ <BR>System.out.println(e); <BR>} <BR>} <BR><BR>public ResultSet execute(String sql){ <BR>rs=null; <BR>try{ <BR><BR>rs=stmt.executeQuery(sql); <BR>} <BR>catch(SQLException e){ <BR>System.out.println(e.getMessage()); <BR>} <BR>return rs; <BR>} <BR><BR>public Connection getConnection(){ <BR>return conn; <BR>} <BR><BR>public boolean close(){ <BR>try{ <BR>if(this.rs!=null){ <BR>this.rs.close(); <BR>} <BR>if(stmt!=null){ <BR>this.stmt.close(); <BR>} <BR>if(conn!=null){ <BR>this.conn.close(); <BR>} <BR>return true; <BR>}catch(Exception err){ <BR>return false; <BR>} <BR>} <BR><BR>} <BR>//======================================= <BR>package product; <BR>import java.sql.*; <BR>public class catalog <BR>{ <BR>ConnectDB conn=new ConnectDB(); <BR>String str=null; <BR>String sql=null; <BR>ResultSet rs=null; <BR>String AncestorID,LinkStr; <BR><BR>public String getCatalog(){ <BR><BR>str="<select name=\"FatherID\" class=\"p9\">\n"+ <BR>"<option value=\"-1\" selected>请选择父类</option>\n"; <BR>String sql=null; <BR>sql="select * from catalog order by AncestorID,Linkstr"; <BR>ResultSet rs=conn.execute(sql); <BR>try{ <BR>while (rs.next()) { <BR>str=str+"<option value="+rs.getString("Productid")+">"; <BR>int nbspCount=rs.getString("LinkStr").length()-1; <BR>for(int i=0;i<nbspCount;i++){ <BR>str=str+" "; <BR>} <BR>if(nbspCount>0) <BR>str=str+"┠"+rs.getString("title")+"</option>\n"; <BR>else <BR>str=str+rs.getString("title")+"</option>\n"; <BR><BR>} <BR>str=str+"</select>"; <BR>rs.close(); <BR>} <BR>catch(SQLException e){ <BR>System.out.println(e.getMessage()); <BR>} <BR>return str; <BR>} <BR>public void setCatalog(String CatalogName,String FatherID){ <BR>int ChildNum=0; <BR>if(FatherID.compareTo("-1")!=0){ <BR>sql="select AncestorID,ChildNum,linkstr from catalog where productid="+FatherID; <BR>//out.println(sql); <BR>try{ <BR>rs=conn.execute(sql); <BR>if(rs.next()){ <BR>AncestorID=rs.getString("AncestorID"); <BR>ChildNum=rs.getInt("ChildNum"); <BR>LinkStr=rs.getString("linkstr"); <BR>} <BR>else{ <BR>AncestorID=""; <BR>LinkStr="1"; <BR>ChildNum=0; <BR>} <BR>LinkStr=LinkStr+(ChildNum+1); <BR>//out.println(AncestorID+":"+LinkStr+":"+ChildNum); <BR>sql="insert into catalog(title,fatherID,AncestorID,LinkStr) values(''"+CatalogName+"'',''"+FatherID+"'',''"+AncestorID+"'',''"+LinkStr+"'')"; <BR>//out.println(sql); <BR>conn.execute(sql); <BR>sql="update catalog set childNum="+(ChildNum+1)+" where productID="+FatherID; <BR>//out.println("<br>"+sql); <BR>conn.execute(sql); <BR>}catch(Exception e){ <BR>System.out.println(e); <BR>} <BR><BR>}else{ <BR>sql="insert into catalog(title,fatherID,LinkStr) values(''"+CatalogName+"'',''"+FatherID+"'',''1'')"; <BR>//out.println(sql); <BR>try{ <BR>conn.execute(sql); <BR>sql="select top 1 productID from catalog order by productID desc "; <BR>rs=conn.execute(sql); <BR>if(rs.next()){ <BR>AncestorID=rs.getString("productID"); <BR>} <BR>else{ <BR>AncestorID=""; <BR>} <BR>rs.close(); <BR>sql="update catalog set AncestorID=''"+AncestorID+"'' where productID="+AncestorID; <BR>//out.println(sql); <BR>conn.execute(sql); <BR>}catch(Exception e){ <BR>System.out.println(e); <BR><BR>} <BR><BR>}//end if <BR><BR>if(rs!=null){ <BR>try{ <BR>rs.close(); <BR>}catch(Exception e){ <BR>System.out.println(e); <BR>} <BR>} <BR><BR>} <BR><BR>public void deleteCatalog(String FatherID){ <BR>int tempData=0; <BR>sql="select fatherID,linkstr,AncestorID from catalog where productID="+FatherID; <BR>rs=conn.execute(sql); <BR>try{ <BR>if(rs.next()){ <BR>tempData=rs.getInt("fatherID"); <BR>LinkStr=rs.getString("linkstr"); <BR>AncestorID=rs.getString("AncestorID"); <BR>} <BR><BR>try{rs.close();} <BR>catch(Exception e){ <BR>System.out.println(e); <BR>} <BR>sql="delete from catalog where ProductID="+FatherID; <BR>// out.println(sql); <BR>try{ <BR>conn.execute(sql); <BR>}catch(Exception e){ <BR>System.out.println(e); <BR>} <BR>sql="delete from catalog where linkstr like ''"+LinkStr+"%'' and AncestorID="+AncestorID; <BR>//out.println(sql); <BR>try{ <BR>conn.execute(sql); <BR>}catch(Exception e){ <BR>System.out.println(e); <BR>} <BR>sql="update catalog set ChildNum=ChildNum-1 where productID="+tempData; <BR>//out.println(sql); <BR>try{ <BR>conn.execute(sql); <BR>}catch(Exception e){ <BR>System.out.println(e); <BR>} <BR>}catch(Exception e){ <BR>System.out.println(e); <BR>} <BR>} //end public <BR><BR>public void updateCatalog(int id){ <BR><BR>} <BR><BR>public String getCatalog(int id){ <BR><BR>str="<select name=\"FatherID\" class=\"p9\">\n"+ <BR>"<option value=\"-1\" >请选择父类</option>\n"; <BR>String sql=null; <BR>sql="select * from catalog order by AncestorID,Linkstr"; <BR>ResultSet rs=conn.execute(sql); <BR>try{ <BR>while (rs.next()) { <BR>int ProductID=rs.getInt("productID"); <BR>if (ProductID==id) { <BR>str=str+"<option value="+ProductID+" selected>"; <BR>} <BR>else{ <BR>str=str+"<option value="+ProductID+">"; <BR>} <BR>int nbspCount=rs.getString("LinkStr").length()-1; <BR>for(int i=0;i<nbspCount;i++){ <BR>str=str+" "; <BR>} <BR>if(nbspCount>0) <BR>str=str+"┠"+rs.getString("title")+"</option>\n"; <BR>else <BR>str=str+rs.getString("title")+"</option>\n"; <BR><BR>} <BR>str=str+"</select>"; <BR>rs.close(); <BR>} <BR>catch(SQLException e){ <BR>System.out.println(e.getMessage()); <BR>} <BR>return str; <BR>} <BR><BR>public String getCatalogList(){ <BR><BR>str="<select name=\"FatherID\" class=\"p9\" size=\"20\" onClick=\"MM_jumpMenu(''parent.mainFrame'',this,0)\">\n"+ <BR>"<option value=\"-1\" selected>请选择父类</option>\n"; <BR>String sql=null; <BR>sql="select * from catalog order by AncestorID,Linkstr"; <BR>ResultSet rs=conn.execute(sql); <BR>try{ <BR>while (rs.next()) { <BR>str=str+"<option value="+rs.getString("Productid")+">"; <BR>int nbspCount=rs.getString("LinkStr").length()-1; <BR>for(int i=0;i<nbspCount;i++){ <BR>str=str+" "; <BR>} <BR>if(nbspCount>0) <BR>str=str+"┠"+rs.getString("title").trim()+"</option>\n"; <BR>else <BR>str=str+rs.getString("title")+"</option>\n"; <BR><BR>} <BR>str=str+"</select>"; <BR>rs.close(); <BR>} <BR>catch(SQLException e){ <BR>System.out.println(e.getMessage()); <BR>} <BR>return str; <BR>} <BR>} <BR> <BR></FONT></TD></TR>
<TR>
<TD height=5>
<HR align=center color=#cccccc noShade SIZE=1>
</TD></TR></TBODY></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?