📄 树形结构的实现.htm
字号:
<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"><BR>"+
<BR>"<option value="-1" selected>请选择父类</option><BR>";
<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+"&nbsp;"; <BR>}
<BR>if(nbspCount>0)
<BR>str=str+"┠"+rs.getString("title")+"</option><BR>";
<BR>else
<BR>str=str+rs.getString("title")+"</option><BR>";
<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"><BR>"+
<BR>"<option value="-1" >请选择父类</option><BR>";
<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+"&nbsp;"; <BR>}
<BR>if(nbspCount>0)
<BR>str=str+"┠"+rs.getString("title")+"</option><BR>";
<BR>else
<BR>str=str+rs.getString("title")+"</option><BR>";
<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)"><BR>"+
<BR>"<option value="-1" selected>请选择父类</option><BR>";
<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+"&nbsp;"; <BR>}
<BR>if(nbspCount>0)
<BR>str=str+"┠"+rs.getString("title").trim()+"</option><BR>";
<BR>else
<BR>str=str+rs.getString("title")+"</option><BR>";
<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>}
<P><EM><FONT
size=2>在保留http://www.javajia.com原出处的情况下,欢迎转载!</FONT></EM></P>
<P><A
href="http://emailpage.activepower.net/model/send.asp?emailpage_id=26439"
target=_blank></A></P>
<P align=center><A
href="http://www.javajia.com/article.php?id=568#top"><IMG
src="树形结构的实现.files/top.gif"
border=0></A></P></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE>
<P> </P><FONT color=#0772b1><!--</FONT></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -