在表中显示ajax响应值

时间:2014-12-02 05:01:04

标签: javascript html sql ajax

我想在ajax调用期间在表上显示sql查询结果。我编写了在ajax调用期间执行查询的代码但是当我试图在表中显示这些值时,它在div标记上显示空值。这是什么是什么?如何以表格格式显示结果集值。

mycode的:

ViewQuery.jsp

<%-- 
    Document   : ViewQuery
    Created on : Nov 10, 2014, 10:23:48 AM
    Author     : user
--%>

<%@page import="java.util.ArrayList"%>
<%@page import="querywork.ReadXml"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="querywork.DBConnection"%>
<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta charset="UTF-8">
            <title>Zerotype Website Template</title>
            <link rel="stylesheet" href="css/style.css" type="text/css">
                <script lang="javascript">
                    try
                    {

                        var request;


                        function myfunction(str)
                        {
                            try
                            {


                                var url="queryview.jsp"
                                url=url+"?query="+str  
                                url=url+"&sid="+Math.random()

                                if(window.XMLHttpRequest){
                                    request=new XMLHttpRequest();
                                }
                                else if(window.ActiveXObject){
                                    request=new ActiveXObject("Microsoft.XMLHTTP");
                                }

                                try
                                {
                                    request.onreadystatechange=getInfo;
                                    request.open("GET",url,true);
                                    request.send();
                                }
                                catch(e)
                                {
                                    alert("Unable to connect to server");
                                }

                                //     alert(textboxes2)
                                //        alert(select)
                            }
                            catch(err)
                            {
                                alert(err)
                            }
                        }
                        function getInfo(){
                            if(request.readyState==4){
                                var val=request.responseText;
                                document.getElementById('amit').innerHTML=val;
                            }
                        }





                    }
                    catch(err)
                    {}

                </script>
                </head>
                <body>

                </body>
                <body>
                    <form method="POST" action="ViewQuery.jsp">
                        <%

                            ReadXml rxml = new ReadXml();
                            ArrayList al = rxml.readXML(0);
                        %>


                        <font color="white">   Choose query</font></label>
                        </label>

                        <select id="c" name="Chooser" style="width:800px; height:50px" onchange="myfunction(this.value)">
                            <% for (int i = 0; i < al.size(); i++) {
%><option><font color="white"><%=al.get(i)%></font></option>
                            <% }%>
                        </select>
                        <p>&nbsp;</p>


                        <p>&nbsp;</p>
                        <p><font color="white">  Result:</font>  <font color="white">   <div id="amit"> </div></font></p>

                    </form>


                </body>
                </html>

queryview.jsp

    <%-- 
        Document   : queryview
        Created on : Nov 16, 2014, 7:15:09 PM
        Author     : user
    --%>
    <%@page import="querywork.getObject"%>
    <%@page import="java.sql.ResultSet"%>
    <%@page import="java.sql.DatabaseMetaData"%>
    <%@page import="querywork.Query"%>
    <%@page import="querywork.Parser"%>
    <%@page import="gudusoft.gsqlparser.EDbVendor"%>
    <%@page import="querywork.gettablecolumns"%>
    <%@page import="java.sql.PreparedStatement"%>
    <%@page import="java.sql.DriverManager"%>
    <%@page import="java.sql.Connection"%>

    <%@page import="querywork.DBConnection"%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>

            <%

                String responseText = new String("");
                boolean flg = false;
                String query1 = (String) request.getParameter("query");
    String[] colname=new String[100];
                EDbVendor dbVendor = EDbVendor.dbvmysql;
                try {

                    getObject g = new getObject(dbVendor);

                    Parser parser1 = new Parser();
                    Parser.Node parsedNode1 = parser1.parse(query1);
                    g.setSqltext(query1);
                    System.out.println(query1);

                    String table = g.run();

                    String[] splitstring = table.split("Columns:");

                    String newval = splitstring[1].replaceAll("\\([^\\)]*\\)", "");

                    String[] inputSplitNewLine = newval.split("\\\n");

    int i1=0;

                    String[] firstval = splitstring[0].split("\\\n");
                    Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/", "root", "");
                    DatabaseMetaData dbmd = conn.getMetaData();
                    responseText= "<table border=1 cellpadding =2 cellspacing=3><tr>";
                    for(int p=0;p<inputSplitNewLine.length;p++)
                                           {
                        String col=inputSplitNewLine[p];

    responseText=responseText+"  <td> "+col+" </td>";
                    }
                    responseText="</tr>";
                    ResultSet ctlgs = dbmd.getCatalogs();
                    for (int i = 1; i < firstval.length; i++) {
                        while (ctlgs.next()) {


                            String dbname = ctlgs.getString(1);

                            DBConnection dbc = new DBConnection(dbname);
                            ResultSet rs = dbc.SelectTable();
                            while (rs.next()) {
                                String columnName = "";
                                String tableName = rs.getString("TABLE_NAME");
                                if (tableName.equalsIgnoreCase(firstval[i])) {
                                    DBConnection dbc1 = new DBConnection(dbname);
                                    ResultSet rs1 = dbc1.execute(query1);

                                    while(rs1.next()) {
                                        out.println(rs1.getString(1));
                                         responseText=responseText +"<tr><td> "+rs1.getString(1)+"</td></tr>";
                                    }
                                }
                            }
                        }
                    }
                    out.print(responseText);
                } catch (Exception e) {
                    out.print(e);
                }

            %>
        </head>
        <body>

        </body>
    </html>

1 个答案:

答案 0 :(得分:0)

检查获取数据是否为空。 在将数据打印到表格单元格之前,用于验证获取数据是否有效或不为空的条件,空值在单元格中添加空格。

示例代码: -

while(rs.next())
{
    %>
     <tr>
    <td><%= (rs.getString(1) !='')?rs.getString(1):"-" %></td> 
    <td><%= (rs.getString(2) !='')?rs.getString(2):"-" %></td> 
    <%  
}

您的代码: - 仅代码编写

        <%-- 
                Document   : queryview
                Created on : Nov 16, 2014, 7:15:09 PM
                Author     : user
            --%>
            <%@page import="querywork.getObject"%>
            <%@page import="java.sql.ResultSet"%>
            <%@page import="java.sql.DatabaseMetaData"%>
            <%@page import="querywork.Query"%>
            <%@page import="querywork.Parser"%>
            <%@page import="gudusoft.gsqlparser.EDbVendor"%>
            <%@page import="querywork.gettablecolumns"%>
            <%@page import="java.sql.PreparedStatement"%>
            <%@page import="java.sql.DriverManager"%>
            <%@page import="java.sql.Connection"%>

            <%@page import="querywork.DBConnection"%>
            <%@page contentType="text/html" pageEncoding="UTF-8"%>

                    <%

                        String responseText = new String("");
                        boolean flg = false;
                        String query1 = (String) request.getParameter("query");
            String[] colname=new String[100];
                        EDbVendor dbVendor = EDbVendor.dbvmysql;
                        try {

                            getObject g = new getObject(dbVendor);

                            Parser parser1 = new Parser();
                            Parser.Node parsedNode1 = parser1.parse(query1);
                            g.setSqltext(query1);
                            System.out.println(query1);

                            String table = g.run();

                            String[] splitstring = table.split("Columns:");

                            String newval = splitstring[1].replaceAll("\\([^\\)]*\\)", "");

                            String[] inputSplitNewLine = newval.split("\\\n");

            int i1=0;

                            String[] firstval = splitstring[0].split("\\\n");
                            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost/", "root", "");
                            DatabaseMetaData dbmd = conn.getMetaData();
                            responseText= "<table border=1 cellpadding =2 cellspacing=3><tr>";
                            for(int p=0;p<inputSplitNewLine.length;p++)
                                                   {
                                String col=inputSplitNewLine[p];

            responseText=responseText+"  <td> "+col+" </td>";
                            }
                            responseText="</tr>";
                            ResultSet ctlgs = dbmd.getCatalogs();
                            for (int i = 1; i < firstval.length; i++) {
                                while (ctlgs.next()) {


                                    String dbname = ctlgs.getString(1);

                                    DBConnection dbc = new DBConnection(dbname);
                                    ResultSet rs = dbc.SelectTable();
                                    while (rs.next()) {
                                        String columnName = "";
                                        String tableName = rs.getString("TABLE_NAME");
                                        if (tableName.equalsIgnoreCase(firstval[i])) {
                                            DBConnection dbc1 = new DBConnection(dbname);
                                            ResultSet rs1 = dbc1.execute(query1);

                                            while(rs1.next()) {
                                                out.println(rs1.getString(1));
                                                 responseText=responseText +"<tr><td> "+rs1.getString(1)+"</td></tr>";
                                            }
                                        }
                                    }
                                }
                            }
                            out.print(responseText);
                        } catch (Exception e) {
                            out.print(e);
                        }

                    %>