0

I want to display the databases in MySQL in a tree structure.

I have a JSP page to get the database name and JavaScript page to display it in tree format.

As I am new to web application development I don't know how to get the database name from JSP page to JavaScript page without using jQuery. Do I need to use jQuery for this purpose?

demoframeset.html

<!--------------------------------------------------------------->
<!-- Copyright (c) 2006 by Conor O'Mahony.                     -->
<!-- For enquiries, please email [email protected].        -->
<!-- Please keep all copyright notices below.                  -->
<!-- Original author of TreeView script is Marcelino Martins.  -->
<!--------------------------------------------------------------->
<!-- This document includes the TreeView script.  The TreeView -->
<!-- script can be found at http://www.TreeView.net.  The      -->
<!-- script is Copyright (c) 2006 by Conor O'Mahony.           -->
<!--------------------------------------------------------------->
<!-- Instructions:                                             -->
<!--   - Through the <STYLE> tag you can change the colors and -->
<!--     types of fonts to the particular needs of your site.  -->
<!--   - A predefined block with black background has been     -->
<!--     made for stylish people :-)                           -->
<!--------------------------------------------------------------->

<HEAD>

    <!-- This is the <STYLE> block for the default styles.  If   -->
    <!-- you want the black background, remove this <STYLE>      -->
    <!-- block.                                                  -->
    <STYLE>
        BODY {
            background-color: white;}
        TD {
            font-size: 10pt; 
            font-family: verdana,helvetica; 
            text-decoration: none;
            white-space:nowrap;}
        A {
            text-decoration: none;
            color: black;}
        .specialClass {
            font-family:garamond; 
            font-size:12pt;
            color:green;
            font-weight:bold;
            text-decoration:underline}
        </STYLE>

        <!-- If you want the black background, replace the contents  -->
        <!-- of the <STYLE> tag above with the following...
          BODY {
            background-color: black;}
          TD {
            font-size: 10pt; 
            font-family: verdana,helvetica; 
            text-decoration: none;
            white-space:nowrap;}
          A {
            text-decoration: none;
            color: white;}
        <!-- This is the end of the <STYLE> contents.                -->

        <!-- Code for browser detection. DO NOT REMOVE.              -->
        <SCRIPT src="ua.js"></SCRIPT>

        <!-- Infrastructure code for the TreeView. DO NOT REMOVE.    -->
        <SCRIPT src="ftiens4.js"></SCRIPT>

        <!-- Scripts that define the tree. DO NOT REMOVE.            -->
        <SCRIPT src="demoFramesetNodes.js"></SCRIPT>

    </HEAD>

    <BODY topmargin="16" marginheight="16">

        <!------------------------------------------------------------->
        <!-- IMPORTANT NOTICE:                                       -->
        <!-- Removing the following link will prevent this script    -->
        <!-- from working.  Unless you purchase the registered       -->
        <!-- version of TreeView, you must include this link.        -->
        <!-- If you make any unauthorized changes to the following   -->
        <!-- code, you will violate the user agreement.  If you want -->
        <!-- to remove the link, see the online FAQ for instructions -->
        <!-- on how to obtain a version without the link.            -->
        <!------------------------------------------------------------->
        <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:silver" href="http://www.treemenu.net/" target=_blank>Javascript Tree Menu</A></FONT></TD></TR></TABLE></DIV>

    <!-- Build the browser's objects and display default view  -->
    <!-- of the tree.                                          -->
    <SCRIPT>initializeDocument()</SCRIPT>
    <NOSCRIPT>
    A tree for site navigation will open here if you enable JavaScript in your browser.
    </NOSCRIPT>

</BODY>

demoFrameSetNodes.js

  USETEXTLINKS = 1
  STARTALLOPEN = 0
   ICONPATH = ''
  foldersTree = gFld("<i>Databases</i>", "demoFramesetRightFrame.html")
  foldersTree.treeID = "Frameset" 
  aux11 = insFld(foldersTree, gFld("New", "Databases.jsp"))

  var xmlHttp 
 function create()
  { 
      xmlHttp=CreateXmlHttpObject()
      if (xmlHttp==null)
      {
         alert ("Browser does not support HTTP Request")
         return
      } 

       var url="new1.jsp"
      url=url+"?dbname="+str

      url=url+"&sid="+Math.random()
     //  out.print(url)
       xmlHttp.onreadystatechange=stateChanged 
       xmlHttp.open("GET",url,true)
       xmlHttp.send()

   }
  function stateChanged() 
   { 
      if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
      { 
          document.getElementById("div").innerHTML=xmlHttp.responseText 
     } 
  } 
  function CreateXmlHttpObject()
  { 
       var objXMLHttp=null
       if (window.XMLHttpRequest)
       {
              objXMLHttp=new XMLHttpRequest()
         }
          else if (window.ActiveXObject)
         {
          objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
        }
         return objXMLHttp
  }

new1.jsp

try {
    String responseText = "";
    String text = "";
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/", "root", "");

    DatabaseMetaData dbmd = conn.getMetaData();

    ResultSet ctlgs = dbmd.getCatalogs();
    while (ctlgs.next()) {
        text += ctlgs.getString(1) + ",";
    }
} catch (Exception e) {
    out.println(e);
}

1 Answer 1

1

You should be able to retrieve the dbname from the request query string parmaters. This can be done using:

request.getParameter("your-param");

In your case, it should be the dbname parameter, which you are injecting in URL within your JS file:

try {
    String dbName = request.getParameter("dbname"); // Retrive the dbname parameter
    String responseText = "";
    String text = "";
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/" + dbName /*Use the dbName in the schema URL*/, "root", "");

    DatabaseMetaData dbmd = conn.getMetaData();

    ResultSet ctlgs = dbmd.getCatalogs();
    while (ctlgs.next()) {
        text += ctlgs.getString(1) + ",";
    }
} catch (Exception e) {
    out.println(e);
}

Edit:

Otherwise you can simply show your database names within your JSP without any external requests calls, since you are already populating your db names to a String delimited by a comma, you can use the <c:forTokens> tag to iterate over a splitted array from you String; text:

<c:forTokens items="${text}" delims="," var="dbname"> 
  <c:out value="${dbname}"/><p> 
</c:forTokens>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.