网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>JAVA>>新手入门>>基础入门>查看文档  
  树形结构的实现     
  文章作者:未知  文章来源:水木森林  
  查看:156次  录入:管理员--2007-11-17  
 
本例采用access做为实例,所有还有很多要改进的地方。 
请读者自已改进 

package product; 
import java.sql.*; 
public class connectdb{ 
string strdbdriver="sun.jdbc.odbc.jdbcodbcdriver"; 
string strconnstr="jdbc:odbc:product"; 
connection conn=null; 
resultset rs=null; 
statement stmt=null; 
// statement stmt=null; 
//构造函数 
public connectdb(){ 
try{ 
class.forname(strdbdriver); conn=drivermanager.getconnection(strconnstr); 
stmt=conn.createstatement(); 

catch(exception e){ 
system.out.println(e); 



public resultset execute(string sql){ 
rs=null; 
try{ 

rs=stmt.executequery(sql); 

catch(sqlexception e){ 
system.out.println(e.getmessage()); 

return rs; 


public connection getconnection(){ 
return conn; 


public boolean close(){ 
try{ 
if(this.rs!=null){ 
this.rs.close(); 

if(stmt!=null){ 
this.stmt.close(); 

if(conn!=null){ 
this.conn.close(); 

return true; 
}catch(exception err){ 
return false; 




//======================================= 
package product; 
import java.sql.*; 
public class catalog 

connectdb conn=new connectdb(); 
string str=null; 
string sql=null; 
resultset rs=null; 
string ancestorid,linkstr; 

public string getcatalog(){ 

str="<select name="fatherid" class="p9">
"+ 
"<option value="-1" selected>请选择父类</option>
"; 
string sql=null; 
sql="select * from catalog order by ancestorid,linkstr"; 
resultset rs=conn.execute(sql); 
try{ 
while (rs.next()) { 
str=str+"<option value="+rs.getstring("productid")+">"; 
int nbspcount=rs.getstring("linkstr").length()-1; 
for(int i=0;i<nbspcount;i++){ 
str=str+" "; 

if(nbspcount>0) 
str=str+"┠"+rs.getstring("title")+"</option>
"; 
else 
str=str+rs.getstring("title")+"</option>
"; 


str=str+"</select>"; 
rs.close(); 

catch(sqlexception e){ 
system.out.println(e.getmessage()); 

return str; 

public void setcatalog(string catalogname,string fatherid){ 
int childnum=0; 
if(fatherid.compareto("-1")!=0){ 
sql="select ancestorid,childnum,linkstr from catalog where productid="+fatherid; 
//out.println(sql); 
try{ 
rs=conn.execute(sql); 
if(rs.next()){ 
ancestorid=rs.getstring("ancestorid"); 
childnum=rs.getint("childnum"); 
linkstr=rs.getstring("linkstr"); 

else{ 
ancestorid=""; 
linkstr="1"; 
childnum=0; 

linkstr=linkstr+(childnum+1); 
//out.println(ancestorid+":"+linkstr+":"+childnum); 
sql="insert into catalog(title,fatherid,ancestorid,linkstr) values(""+catalogname+"",""+fatherid+"",""+ancestorid+"",""+linkstr+"")"; 
//out.println(sql); 
conn.execute(sql); 
sql="update catalog set childnum="+(childnum+1)+" where productid="+fatherid; 
//out.println("<br>"+sql); 
conn.execute(sql); 
}catch(exception e){ 
system.out.println(e); 


}else{ 
sql="insert into catalog(title,fatherid,linkstr) values(""+catalogname+"",""+fatherid+"","1")"; 
//out.println(sql); 
try{ 
conn.execute(sql); 
sql="select top 1 productid from catalog order by productid desc "; 
rs=conn.execute(sql); 
if(rs.next()){ 
ancestorid=rs.getstring("productid"); 

else{ 
ancestorid=""; 

rs.close(); 
sql="update catalog set ancestorid=""+ancestorid+"" where productid="+ancestorid; 
//out.println(sql); 
conn.execute(sql); 
}catch(exception e){ 
system.out.println(e); 



}//end if 

if(rs!=null){ 
try{ 
rs.close(); 
}catch(exception e){ 
system.out.println(e); 





public void deletecatalog(string fatherid){ 
int tempdata=0; 
sql="select fatherid,linkstr,ancestorid from catalog where productid="+fatherid; 
rs=conn.execute(sql); 
try{ 
if(rs.next()){ 
tempdata=rs.getint("fatherid"); 
linkstr=rs.getstring("linkstr"); 
ancestorid=rs.getstring("ancestorid"); 


try 
catch(exception e){ 
system.out.println(e); 

sql="delete from catalog where productid="+fatherid; 
// out.println(sql); 
try{ 
conn.execute(sql); 
}catch(exception e){ 
system.out.println(e); 

sql="delete from catalog where linkstr like ""+linkstr+"%" and ancestorid="+ancestorid; 
//out.println(sql); 
try{ 
conn.execute(sql); 
}catch(exception e){ 
system.out.println(e); 

sql="update catalog set childnum=childnum-1 where productid="+tempdata; 
//out.println(sql); 
try{ 
conn.execute(sql); 
}catch(exception e){ 
system.out.println(e); 

}catch(exception e){ 
system.out.println(e); 

} //end public 

public void updatecatalog(int id){ 



public string getcatalog(int id){ 

str="<select name="fatherid" class="p9">
"+ 
"<option value="-1" >请选择父类</option>
"; 
string sql=null; 
sql="select * from catalog order by ancestorid,linkstr"; 
resultset rs=conn.execute(sql); 
try{ 
while (rs.next()) { 
int productid=rs.getint("productid"); 
if (productid==id) { 
str=str+"<option value="+productid+" selected>"; 

else{ 
str=str+"<option value="+productid+">"; 

int nbspcount=rs.getstring("linkstr").length()-1; 
for(int i=0;i<nbspcount;i++){ 
str=str+" "; 

if(nbspcount>0) 
str=str+"┠"+rs.getstring("title")+"</option>
"; 
else 
str=str+rs.getstring("title")+"</option>
"; 


str=str+"</select>"; 
rs.close(); 

catch(sqlexception e){ 
system.out.println(e.getmessage()); 

return str; 


public string getcataloglist(){ 

str="<select name="fatherid" class="p9" size="20" onclick="mm_jumpmenu("parent.mainframe",this,0)">
"+ 
"<option value="-1" selected>请选择父类</option>
"; 
string sql=null; 
sql="select * from catalog order by ancestorid,linkstr"; 
resultset rs=conn.execute(sql); 
try{ 
while (rs.next()) { 
str=str+"<option value="+rs.getstring("productid")+">"; 
int nbspcount=rs.getstring("linkstr").length()-1; 
for(int i=0;i<nbspcount;i++){ 
str=str+" "; 

if(nbspcount>0) 
str=str+"┠"+rs.getstring("title").trim()+"</option>
"; 
else 
str=str+rs.getstring("title")+"</option>
"; 


str=str+"</select>"; 
rs.close(); 

catch(sqlexception e){ 
system.out.println(e.getmessage()); 

return str; 

 
 
上一篇: 让第一个jdo的应用跑起来    下一篇: 是否能让java 和 .net框架共存
  相关文档
sun认证java2程序员考试(scjp) 试题解析 11-16
java基础:深入浅出java的访问者模式 11-17
面向对象在软件重用和体系结构中的应用 11-16
如何在jbossserver上发布ejb 11-17
实例讲解java中的布局管理器的使用方法 11-16
j2ee核心:ibatis dao入门与进阶一 11-17
java io 包中的decorator模式 11-16
如何搭建j2ee开发环境 11-20
线程控制动画 11-16
用jdring设置类似cron的日程提醒 11-16
使用jdbc创建数据库访问程序 11-16
开放源码-smtp发信客户端 for java 11-17
eclipse工具下开发struts解决乱码的问题 06-13
toexponential 方法 11-16
java非对称加密的源代码(rsa) 11-17
为rmi实现类jini的发现机制 11-17
jdbc连接数据库经验技巧集萃 11-16
休闲程序空间:趣说java处理异常的方式 11-16
Java入门:java类文件详解 03-25
给你的java应用程序加上日志功能 11-17
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
技术电话:13616026886
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息