当前位置:Gxlcms > mysql > 产品目录/论坛树形结构的实现

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

时间:2021-07-01 10:21:17 帮助过:21人阅读

原理请参见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;
}
}

人气教程排行