javascript - Display ajax response value in table -


i want display sql query result on table during ajax call.i wrote code executes query during ajax call when tried display these values in table displays null value on div tag.what reason?how can display result set value in table format.

mycode:

viewquery.jsp

<%--      document   : viewquery     created on : nov 10, 2014, 10:23:48     author     : user --%>  <%@page import="java.util.arraylist"%> <%@page import="querywork.readxml"%> <%@page import="java.sql.resultset"%> <%@page import="querywork.dbconnection"%> <!-- change template, choose tools | templates , open template in 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 connect 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)">                             <% (int = 0; < 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();                     (int = 1; < 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> 

check whether getting data null or not. before printing data table cell put condition verification whether fetch data valid or not or not null, null value add space in cell.

example code:-

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

your code :- write following code

        <%--                  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();                             (int = 1; < 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);                         }                      %> 

Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -