PostgreSQL数据库查询在JSP查询中丢弃

时间:2017-02-23 06:26:53

标签: jsp postgresql-9.1

所以我正在开发一个postgres数据库。我正在使用JSP将数据库连接到我的html页面。我从select标签发送查询,一旦我收到回复,我就将geojson附加到传单地图上。奇怪的是,查询工作的时间是一半,但其他时间则失败。我正在使用Mars Eclipse来测试程序。我还在许多浏览器上查看了输出。我在localhost上运行。有人知道为什么查询有时会起作用而不是其他人吗?我从来没有见过这样的编程。它一直有效或无效。这看起来确实很奇怪。此外,我将响应作为innerHTML返回,然后根据数组中的需要执行一些脚本来格式化数据。我知道必须有一个更好的方法来回复,但这是我的第一个数据库应用程序之一,我很高兴看到地图上的点。如果有人对如何将我的观点放在地图上有任何更好的想法,请随时分享。但是,我唯一真正关心的是查询不时丢失的事实。也许它与它在本地机器而不是实际服务器上运行的事实有关 - 我不知道。这是代码,感谢任何有用的回复。

HTML

<html><head>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />

<link rel="stylesheet" href="myCSS.css"> 
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>

<script src="https://unpkg.com/leaflet@1.0.3/dist/leaflet.js"></script>
<script>
myGlobalArray = [];
myArrayResponse = [];
j=0;
function myFun(){
    var map = L.map('map',{ center: [41.77, -87.5333], zoom: 9});
    L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', { attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>' }).addTo(map);
    myGlobalArray.push(map);
 }




function showUser(str, myInt) {

if (str == "categories") {return;} 

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

    xmlhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
           var theDiv = document.getElementById("map");

           myArrayResponse = this.responseText.split(";");


myArrayResponse.splice(0,1);myArrayResponse.splice(myArrayResponse.length-1,1);
        myArrayFun(str);    
      }
    };
if(myInt == 1){xmlhttp.open("GET","callQuery.jsp?q="+str,true);}



    xmlhttp.send();

 }
}


function myArrayFun(theStr){
    alert(myArrayResponse[1]);
    if(typeof(myArrayResponse[1])== "undefined"){alert(theStr);
     //showUser(theStr,1);
     //function call above become infinite loop if input fails
     }





  var geoF = [JSON.parse(myArrayResponse[1]),{"type":"Point","coordinates":[-87.723, 41.5]}];

  var geojsonMarkerOptions = {radius: 3,fillColor: "#ff7800",color: "#000",weight: 1,opacity: 1,fillOpacity: 0.8};
  var thegeoj;
  for(r=0;r<myArrayResponse.length;r++){
   i = r;
   try {

    var theP = JSON.parse(myArrayResponse[r]);
    var thegeoj=    L.geoJSON(theP, {
        pointToLayer: function (feature, latlng) {
            return L.circleMarker(latlng, geojsonMarkerOptions);
        }
    }).addTo(myGlobalArray[0]);
    } catch(err) {

    }

 }
 alert(i);

 }


 </script>

 </head>






 <body onload="myFun()">
 <div id="header" onclick="myArrayFun()"><strong><font color="black">  

 <h1> <center>WEATHER APPLICATION</center></h1> </font></strong>
 </div>


 <div id="map" data-dojo-type="dijit/layout/ContentPane" data-dojo-props="region:'center'"></div>

 <div id="leftContainer" >
<select id="myCat" onchange="showUser(this.value, 1)">
    <option value="categories">Categories</option>
    <option value="HOMICIDE">Homicide</option>
    <option value="PROSTITUTION">Prostitution</option>
    <option value="ARSON">Arson</option>
</select>     



</div>



<div id="footer"><center>Last Updated January 28, 2017!</center></div> 


</body>
</html>

JSP

<%@page import="java.text.SimpleDateFormat"%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%@page import="javax.servlet.*" %>

<%@page import="javax.servlet.http.*" %>

<%@page import="java.util.*,java.sql.*,java.io.*" %>



<html>

<%!Connection con1; %>

<%!Statement s1; %>

<%!ResultSet rs1; %>

<% String name=request.getParameter("q");

try{

Class.forName("org.postgresql.Driver");

con1=DriverManager.getConnection

("jdbc:postgresql://localhost:5432/chicagoTest","postgres","pass123");

s1=con1.createStatement();

rs1=s1.executeQuery("select ST_AsGeoJSON(geom) from myschema.spatialdata2 where ptype='"+name+"'");

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

%>;

<% while(rs1.next())

{ %>
<%=rs1.getString(1)%>;



<% } %>

</html>

0 个答案:

没有答案