JSP Treeview

Hi Friends, I want to create the simple tree view which display the database fields as tree structure in jsp.I have to display the database field values for example, at first i have to dispaly clients tree, if i click the client i have to display the projects of the client, etc. I want to use simple jsp code not the using jsp custom tags. I have to dispaly a tree from database then inside value, etc in tree structure.I dont want to use JSTL or EL. Just i want to show Root Node, Parent Node, Child Node from mysql database using simple jsp. I have pasted the jsp code regarding this, but this is complex to apply to my requirement. If anyone change this code and send me the simple code with database access with treeview its nice. Thanks in advance. Regards, Mike


<%@ page contentType="text/html; charset=iso-8859-1" language="java" import="java.sql.*,java.util.StringTokenizer,java.util.*,java.text.*" errorPage="" %> 

<% 
          
             Connection conn = null; 
             Class.forName("com.mysql.jdbc.Driver"); 

         try{ 
         conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/treeview","root","root"); 
		System.out.println("conn is " + conn);
         String tempNodeID=nullconv(request.getParameter("NodeID")); 
		System.out.println("tempNodeID is " +tempNodeID);
         int tempdownline=nullIntconv(nullconv(request.getParameter("downline"))); 
		 System.out.println("tempdownline is " +tempdownline);
         setConnection(conn,out,tempNodeID,tempdownline); 
		 System.out.println("after setConnection " );

         } 
         catch(Exception e) 
         { 
           e.printStackTrace(); 
         } 
%> 

<%! 
public String nullconv(String str) 
{ 
   if(str==null) 
      str=""; 
   else if(str.equalsIgnoreCase("null")) 
      str=""; 
   else if((str.trim()).equals("")) 
      str=""; 
   return str; 
} 
public int nullIntconv(String inv) 
   { 
      if(inv==null) 
      { 
         inv="0"; 
      } 
      else if((inv.trim()).equals("null")) 
      { 
         inv="0"; 
      } 
      else if(inv.equals("")) 
      { 
         inv="0"; 
      } 
       
      return Integer.parseInt(inv); 
   } 


      Connection con= null;    

      PreparedStatement pstmt = null; 
      PreparedStatement pstmt1 = null; 

      ResultSet rs = null; 
      ResultSet rs1 = null; 

      JspWriter out=null; 
      String   NodeID ="1"; 
      int downline = 0; 
       
   public void setConnection(Connection con,JspWriter ot,String rq,int dwline) 
   { 
      this.con=con; 
      try{ 
      this.out=ot; 
      NodeID=rq; 
      downline=dwline; 
     
       if(NodeID.equals("")) 
         { 
          NodeID="1";  ///////////////////// change this according to your requirement 
         } 
         if(downline==0) 
         { 
          downline=9;   ////////////////////// this is for advance requirement should use according to your requirement 
         } 
      } 
      catch(Exception e) 
      { 
         System.out.println("IO error :"+e); 
      } 
     
    } 
    
    
   public void outputJavascriptForRoot() 
   { 
             
      String queryString =null; 
       
      queryString ="SELECT NodeID, Name, IsFolder, ParentID, Link,client, project, level FROM nodestable WHERE (NodeID='" + NodeID +"')"; 
      try{ 
       
      pstmt=con.prepareStatement(queryString); 
      rs=pstmt.executeQuery(); 
       
      // It should return one and one only 
      while (rs.next()) 
      { 
        out.print("foldersTree = gFld('<i>tree</i>')" + "\n"); 
        out.print("web" + rs.getString("NodeID") + " = insFld(foldersTree, gFld('" + rs.getString("project")  + "','test_tree.jsp?distibutorid=" + rs.getString("NodeID") + "&downline="+downline+"'))" + "\n"); 
        outputJavascriptForSubFolder(Integer.parseInt(rs.getString("NodeID")), rs.getString("project"),"web" + rs.getString("NodeID"), rs.getString("Link"), downline); 
		} 
       
      // close the DataReader and Connection 
       
       
    
      } 
      catch(SQLException ex) 
      { 
        System.out.println("sqll eroror :"+ex); 
       } 
       catch(Exception e) 
       { 
          System.out.println("error :"+e); 
       } 
   } 
    
    
 public void outputJavascriptForSubFolder(int folderId, String Name, String fName, String Link, int downline) 
   { 
       
   try{ 
      PreparedStatement psQueryString = null; 
      ResultSet rsqueryString = null; 
       String queryString1 ="SELECT NodeID, Name, IsFolder, ParentID, client, project, Link FROM nodestable WHERE ((ParentID=" + folderId + ") AND (IsFolder=1) AND level<=" + downline + ") ORDER BY project"; 
      psQueryString=con.prepareStatement(queryString1); 
      rsqueryString=psQueryString.executeQuery(); 
       
       
      // iterate through the rows in the Nodes table 
         while(rsqueryString.next()) 
         { 
         out.print("web" + rsqueryString.getString("NodeID") + "=insFld(" + fName + ",gFld('" + rsqueryString.getString("project") + "[" + rsqueryString.getString("NodeID") + "]','test_tree.jsp?NodeID="+rsqueryString.getString("NodeID")+"&downline="+downline+"'))  " + "\n" ); 
         outputJavascriptForSubFolder(Integer.parseInt(rsqueryString.getString("NodeID")),rsqueryString.getString("project"),"web" + rsqueryString.getString("NodeID"),rsqueryString.getString("Link"),downline); 
         } 
     } 
   catch(Exception e) 
      { 
        System.out.println(e); 
      } 
   } 
  
%> 


<html> 
<head> 

<title>Tree from database</title> 

<style type="text/css"> 
   BODY {background-color: white} 
   TD {font-size: 10pt; 
       font-family: verdana,helvetica; 
      text-decoration: none; 
      white-space:nowrap;} 
   A  {text-decoration: none; 
       color: black} 
</style> 

<!-- As in a client-side built tree, all the tree infrastructure is put in place 
     within the HEAD block, but the actual tree rendering is trigered within the 
     BODY --> 

<!-- Code for browser detection --> 
<script language="javascript" src="ua.js"></script> 
<script language="javascript" src="ftiens4.js"></script> 
<!-- Execution of the code that actually builds the specific tree. 
     The variable foldersTree creates its structure with calls to 
     gFld, insFld, and insDoc --> 
<script language="javascript"> 
USETEXTLINKS = 1 
STARTALLOPEN = 1 
PRESERVESTATE = 1 
ICONPATH = '' 
HIGHLIGHT = 1 
<% 
outputJavascriptForRoot(); 
%> 


// Load a page as if a node on the tree was clicked (synchronize frames) 
// (Highlights selection if highlight is available.) 
function loadSynchPage(xID) 
{ 
   var folderObj; 
   docObj = parent.treeframe.findObj(xID); 
   docObj.forceOpeningOfAncestorFolders(); 
   parent.treeframe.clickOnLink(xID,docObj.link,'basefrm'); 

    //Scroll the tree window to show the selected node 
    //Other code in these functions needs to be changed to work with 
    //frameless pages, but this code should, I think, simply be removed 
    if (typeof parent.treeframe.document.body != "undefined") //scroll doesn work with NS4, for example 
        parent.treeframe.document.body.scrollTop=docObj.navObj.offsetTop 
} 
</script> 
</head> 

<body topmargin=16 marginheight=16> 

<div style="position:absolute; top:0; left:0; "><table border=0><tr><td><font size=-2><a style="font-size:7pt;text-decoration:none;color:white" href="http://www.treeme.net/" target=_blank>Example For Treeview</a></font></td></tr></table></div> 

<!-- Build the browser's objects and display default view of the 
     tree. --> 
<script language="javascript"> 
initializeDocument() 
//Click the Parakeet link 
loadSynchPage(11) 
</script> 
<noscript> 
A tree for site navigation will open here if you enable JavaScript in your browser. 
</noscript> 
</body> 
</html> 



Join the discussion
Be the first to comment on this article. Our Commenting Policies
See more