Javascript 页面不显示数据库值

Javascript page doesn't display database values

本文关键字:数据库 显示 Javascript      更新时间:2023-09-26

我想以树格式显示每个数据库的所有数据库和表。所以我在我的代码中编写了一个代码 Jsp 页面,获取数据库名称和表名称并将其传递给 javascript 页面。但是在我的代码中,Javascript页面仅显示一个数据库名称。谁能帮我找出原因?我的代码:

左框架集.jsp

 <%-- 
Document   : LeftFrameset
Created on : Nov 13, 2014, 1:10:22 PM
Author     : user
--%>
   <%@page import="querywork.DBConnection"%>
    <%@page import="java.sql.ResultSet"%>
   <%@page import="java.sql.DatabaseMetaData"%>
   <%@page import="java.sql.DriverManager"%>
   <%@page import="java.sql.Connection"%>
  <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <!DOCTYPE html>
<HTML>
<!--------------------------------------------------------------->
<!-- Copyright (c) 2006 by Conor O'Mahony.                     -->
<!-- For enquiries, please email GubuSoft@GubuSoft.com.        -->
<!-- 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>
            <%
        try {
            String responseText = "";
            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()) {
                String text = "";
                String db = ctlgs.getString(1);
                DBConnection dbc = new DBConnection(db);
                ResultSet rs = dbc.SelectTable();
                while (rs.next()) {
                    String tableName = rs.getString("TABLE_NAME");
                    text += tableName + ",";
                }

    %>
    <!--Scripts that define the tree. DO NOT REMOVE.           -->
    <input type="hidden" name="value1" id="hiddenvalue1" value=>
    <input type="hidden" name="value" id="hiddenvalue" value=<%=ctlgs.getString(1)%>>
    <SCRIPT src="demoFramesetNodes.js"></SCRIPT> 

    <%
            }
        } catch (Exception e) {
            out.println(e);
    }%>
    <!-- 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>
    </HTML>

DemoFramesetNodes.js

    var str=document.getElementById("hiddenvalue") .value
   var str1=document.getElementById("hiddenvalue1") .value
   alert (str);
 var table=str1 .split(",");
 USETEXTLINKS = 1
 // Configures whether the tree is fully open upgmon loading of the page, or  whether
// only the root node is visible.
 STARTALLOPEN = 0
 // Specify if the images are in a subdirectory;
  ICONPATH = ''

 foldersTree = gFld("<i>Treeview Demo</i>", "demoFramesetRightFrame.html")
 foldersTree.treeID = "Frameset"
  aux1 = insFld(foldersTree, gFld("New", "Databases.jsp"))
  aux2 = insFld(foldersTree, gFld(str, "http://www.treeview.net/treemenu/demopics/beenthere_america.gif"))
   insDoc(aux2, gLnk("R", "New Table", "CreateTable.jsp?dbname="+str))
     for(var i=0;i<table. length;i++)
    {
     //        alert ('sessionNameVal' + dbname[i]);

      insDoc(aux2, gLnk("R", table[i], "http://www.treeview.net/treemenu/demopics/beenthere_america.gif"))
   }

DBConnection.java

   /*
    * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
      */
    package querywork;

   import java.sql.Connection;
   import java.sql.DatabaseMetaData;
      import java.sql.DriverManager;
  import java.sql.PreparedStatement;
   import java.sql.ResultSet;
     import java.sql.SQLException;
     import java.sql.Statement;
   import java.text.SimpleDateFormat;
   import java.util.ArrayList;
   import java.util.Date;
     import java.util.Locale;
   import java.util.Random;
   import java.util.logging.Level;
   import java.util.logging.Logger;
    import javax.swing.JOptionPane;
/**
 *
     * @author ash
  */
   public class DBConnection {
Connection conn;
Statement st;
ResultSet rs;
PreparedStatement ps,ps1;
String dbname;
public DBConnection(String dbname){
    try{
        this.dbname=dbname;
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        conn=DriverManager.getConnection("jdbc:mysql://localhost/"+dbname,"root","");
        System.out.println("connected to the DB");
    }catch(Exception e){
        e.printStackTrace();
    }
  }
   public ResultSet SelectTable()
   {

           ResultSet rs1 = null;
    boolean flg=true;
     try{

DatabaseMetaData meta = conn.getMetaData();
rs1 = meta.getTables(null, null, null, new String[]{"TABLE"});


    }catch(Exception e){
       e.printStackTrace();
    }
    return rs1;
}
 }

好的,在你的评论之后,我开始认为我理解你的问题。

让我们把与数据库的对话排除在外,因为这不是问题所在。 最终,您的 JSP 将生成类似于以下内容的 HTML:

<input type="hidden" name="value1" id="hiddenvalue1" value=>
<input type="hidden" name="value" id="hiddenvalue" value=information_schema>
<SCRIPT src="demoFramesetNodes.js"></SCRIPT> 
<input type="hidden" name="value1" id="hiddenvalue1" value=>
<input type="hidden" name="value" id="hiddenvalue" value=mysql>
<SCRIPT src="demoFramesetNodes.js"></SCRIPT> 
<input type="hidden" name="value1" id="hiddenvalue1" value=>
<input type="hidden" name="value" id="hiddenvalue" value=user>
<SCRIPT src="demoFramesetNodes.js"></SCRIPT> 
<input type="hidden" name="value1" id="hiddenvalue1" value=>
<input type="hidden" name="value" id="hiddenvalue" value=test>
<SCRIPT src="demoFramesetNodes.js"></SCRIPT> 

(数据库名称可能会有所不同。

这里的问题是每次加载demoFramesetNodes.js时,浏览器都会运行以下行:

var str = document.getElementById("hiddenvalue").value

在您的情况下,您有 7 个元素的 ID hiddenvalue . ID 在 HTML 页面中是唯一的。 我不知道有任何 JavaScript 规范说如果你用不唯一的 ID 调用document.getElementById会发生什么,但如果它是未定义的行为,我不会感到惊讶。 如果是未定义的行为,浏览器将有权返回具有给定ID的第一个元素,最后一个这样的元素,这些元素中的任意一个,甚至null。 在您的情况下,您的浏览器似乎返回了第一个元素。 因此,您会收到七次information_schema警报。

解决此问题的一种方法是在JSP中写出分配给strstr1的赋值,而不是将它们放在隐藏的形式输入中并在demoFramesetNodes.js开始时读出它们。 因此,您的 JSP 将包含以下内容,而不是<input type="hidden" ...>行:

<SCRIPT type="text/javascript">
var str = "<%= ctlgs.getString(1) %>";
var str1 = "<%= text %>";
</SCRIPT>

(您还必须从demoFramesetNodes.js中删除要strstr1的分配。

仔细看看demoFramesetNodes.js,脚本中的一些东西应该只做一次,比如声明常量和创建树的顶级节点,其他东西应该每秒级(即数据库)节点做一次。 我建议:

  • 将每秒级节点需要完成一次的事情移动到函数中,
  • while循环中调用函数,而不是每次都分配给strstr1
  • HEAD 元素中仅加载一次demoFramesetNodes.js

我把这个留给你作为练习。


数据库和表的名称可能只包含字母、数字和下划线,所以你可以将它们的名称直接写到 JavaScript 字符串中,如上所述。 但是,更一般地说,这根本不是一件安全的事情。 例如,假设有人设法创建了一个名称如下的表或数据库

x";document.location='http://www.evil.com/';//

这将导致输出,例如

var str = "x";document.location='http://www.evil.com/';//";

这将导致浏览器导航到 www.evil.com .

显然,这是不可取的,并且是称为跨站点脚本(XSS)的安全漏洞的情况。 在将值写入 JavaScript 之前对其进行转义可以避免此安全问题。