作者 主題: tomcat無法連接連接Postgresql~求助  (閱讀 4677 次)

0 會員 與 1 訪客 正在閱讀本文。

kaisite2004

  • 可愛的小學生
  • *
  • 文章數: 2
    • 檢視個人資料
tomcat無法連接連接Postgresql~求助
« 於: 2013-08-14 21:53 »
<%@ page import="java.sql.*" %>

<h2>Location</h2>
<table border=1>
<tr>
<th>id</th>
<th>lat</th>
<th>lng</th>
<th>time</th>
</tr>
<%
String jdbcDriverName="org.postgresql.Driver";
Driver driver = (Driver) Class.forName(jdbcDriverName).newInstance();
DriverManager.registerDriver(driver);
Connection dbcon = DriverManager.getConnection("jdbc:postgresql:postgres","postgres","123456");
Statement st = dbcon.createStatement();
ResultSet rt = st.executeQuery("select * from test");
while(rt.next())
{
%>

<tr>
<td><%=rt.getString("id")%></td><td><%=rt.getString("lat")%></td><td><%=rt.getString("lng")%></td><td><%=rt.getString("time")%></td>
</tr>
<%
}
%>

<!DOCTYPE html>
<html>
<head>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
    <style type="text/css">
      html { height: 50% }
     
      #map_canvas { height: 100px;width:100px; }
    </style>
    <script type="text/javascript"
      src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false">
    </script>
    <script type="text/javascript">
      function initialize() {
        var mapOptions = {
          center: new google.maps.LatLng(<%=rt.getString("lat")%>, <%=rt.getString("lng")%>),
          zoom: 8,
          mapTypeId: google.maps.MapTypeId.ROADMAP
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"),mapOptions);
         
         marker = new google.maps.Marker
  ({
           position: new google.maps.LatLng(<%=rt.getString("lat")%>, <%=rt.getString("lng")%>),
            map: map
   });
         
      }
    </script>
  </head>
  <body onLoad="initialize()">

    <div id="map_canvas" style="width:100%; height:100%"></div>

  </body>
</html>
<%
rt.close();
st.close();
dbcon.close();
%>


錯誤信息
HTTP Status 500 - An exception occurred processing JSP page /test2.jsp at line 44

type Exception report

message An exception occurred processing JSP page /test2.jsp at line 44

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /test2.jsp at line 44

41:     <script type="text/javascript">
42:       function initialize() {
43:         var mapOptions = {
44:           center: new google.maps.LatLng(<%=rt.getString("lat")%>, <%=rt.getString("lng")%>),
45:           zoom: 8,
46:           mapTypeId: google.maps.MapTypeId.ROADMAP
47:         };


Stacktrace:
   org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
   org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
   org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause

javax.servlet.ServletException: org.postgresql.util.PSQLException: 查詢結果指標位置不正確,您也許需要呼叫 ResultSet 的 next() 方法。
   org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:912)
   org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
   org.apache.jsp.test2_jsp._jspService(test2_jsp.java:157)
   org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
   org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
   org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause

org.postgresql.util.PSQLException: 查詢結果指標位置不正確,您也許需要呼叫 ResultSet 的 next() 方法。
   org.postgresql.jdbc2.AbstractJdbc2ResultSet.checkResultSet(AbstractJdbc2ResultSet.java:2860)
   org.postgresql.jdbc2.AbstractJdbc2ResultSet.getString(AbstractJdbc2ResultSet.java:1955)
   org.postgresql.jdbc2.AbstractJdbc2ResultSet.getString(AbstractJdbc2ResultSet.java:2567)
   org.apache.jsp.test2_jsp._jspService(test2_jsp.java:117)
   org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
   org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
   org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
   org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
   javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.42 logs.

Apache Tomcat/7.0.42

請問查詢結果指標位置不正確,您也許需要呼叫 ResultSet 的 next() 方法。
是位什麼?

h2owater

  • 可愛的小學生
  • *
  • 文章數: 6
    • 檢視個人資料
Re: tomcat無法連接連接Postgresql~求助
« 回覆 #1 於: 2013-10-24 18:27 »
你好!

在你的程式碼
line 18 while(rt.next())

已經在 line 26 跑完了, rt 已經指向null record

在 line 44 再以 rt.getString("lat") 語法便會 throw exception