0

0

产品目录/论坛 树形结构的实现

php中文网

php中文网

发布时间:2016-06-07 15:12:16

|

1348人浏览过

|

来源于php中文网

原创

原理请参见www.chinaasp.com/sqlbbs中的ASP 论坛 版精华区 本例采用access做为实例,所有还有很多要改进的地方。 请读者自已改进 package PRodUCt; import java.sql.*; public class ConnectDB{ String strDBDriver="sun.jdbc.odbc.JdbcOdbcDriver"; String


原理请参见www.chinaasp.com/sqlbbs中的ASP论坛版精华区
本例采用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="";
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("
"+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{rs.close();}
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="";
rs.close();
}
catch(SQLException e){
System.out.println(e.getMessage());
}
return str;
}

public String getCatalogList(){

str="";
rs.close();
}
catch(SQLException e){
System.out.println(e.getMessage());
}
return str;
}
}

相关专题

更多
php代码编辑器入口汇总
php代码编辑器入口汇总

本文整理了主流PHP代码编辑器的官网入口及在线使用链接,阅读专题下面的文章了解更多详细内容。

3

2026.01.04

php代码编辑器地址汇总
php代码编辑器地址汇总

本文整理了主流PHP代码编辑器的官网入口及在线使用链接,阅读专题下面的文章了解更多详细内容。

0

2026.01.04

Excel制作交互图表的方法 Excel交互式的图表教程大全
Excel制作交互图表的方法 Excel交互式的图表教程大全

Excel交互式图表可通过四种方法实现:一、用切片器控制数据透视图;二、结合下拉列表与INDEX-MATCH动态引用;三、用选项按钮绑定图表系列;四、利用动态命名区域配合OFFSET函数。本专题为大家提供相关的文章、下载、课程内容,供大家免费下载体验。

2

2026.01.04

php验证码教程合集
php验证码教程合集

本专题整合了php验证码php验证码教程合集,阅读专题下面的文章了解更多详细内容。

1

2026.01.04

NGINX配置指南
NGINX配置指南

本专题整合了nginx配置相关教程,阅读专题下面的文章了解更多详细内容。

1

2026.01.04

漫画合集pdf网盘入口_漫画解说合集一口气看完
漫画合集pdf网盘入口_漫画解说合集一口气看完

精选高人气漫画合集PDF,一站式网盘入口直达!深度漫画解说整合,一口气看完经典与新作,剧情梳理清晰,省时省力,追漫党必看合集。

9

2026.01.04

Java云原生微服务开发_Java如何开发云原生微服务
Java云原生微服务开发_Java如何开发云原生微服务

Java云原生微服务开发是指 利用Java语言,结合云原生理念和技术(如容器、Kubernetes),将大型应用拆解为一系列独立、小巧、松耦合的微服务,并通过轻量级API进行通信,实现快速开发、部署、弹性伸缩和高效运维的现代化应用开发模式。它融合了微服务架构(将应用拆分)与云原生技术(容器化、编排、自动化),旨在构建高可用、可扩展的分布式系统。

8

2026.01.04

php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

253

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

131

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Webpack4.x---十天技能课堂
Webpack4.x---十天技能课堂

共20课时 | 1.4万人学习

Bootstrap4.x---十天精品课堂
Bootstrap4.x---十天精品课堂

共22课时 | 1.6万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号