顯示文章

這裡允許您檢視這個會員的所有文章。請注意, 您只能看見您有權限閱讀的文章。


主題 - kaisite2004

頁: [1]
1
<%@ 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() 方法。
是位什麼?

2
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <winsock2.h>
#include <mysql.h>/*注意要包含???文件*/
#include <stdio.h>
#include <time.h>
#include<iostream>
using namespace std;
#include<windows.h>

#pragma comment(lib,"libmysql")
/*定?了一些?据??接需要的宏*/
#define HOST "localhost"
#define USERNAME "root"
#define PASSWORD "123456"
#define DATABASE "test"
/*??函?用??行?入的sql語句*/
void exe_sql(char* sql) {
MYSQL my_connection; /*?是一??据??接*/
int res; /*?行sql語句后的返回?志*/
/*初始化mysql?接my_connection*/
mysql_init(&my_connection);
/*?里就是用了mysql.h里的一?函?,用我?之前定?的那些宏建立mysql?接,并
返回一?值,返回不?空?明?接是成功的*/
if (mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD,
DATABASE, 0, NULL, CLIENT_FOUND_ROWS)) {/*?接成功*/
printf("?据??行exe_sql?接成功!n");
/*?句?是?置查????utf8,??支持中文*/
mysql_query(&my_connection, "set names utf8");
/*下面?句?就是用mysql_query函???行我????入的sql語句,
??返回一?int值,如果?0,?明語句?行成功*/
res = mysql_query(&my_connection, sql);
if (res) {/*?在就代表?行失?了*/
printf("Error: mysql_query !\n");
/*不要忘了???接*/
mysql_close(&my_connection);
} else {/*?在就代表?行成功了*/
/*mysql_affected_rows?返回?行sql后影?的行?*/
printf("%d 行受到影?!\n",
mysql_affected_rows(&my_connection));
/*不要忘了???接*/
mysql_close(&my_connection);
}
} else {
/*?据??接失?*/
printf("?据??行exe_sql?接失?!\n");
}
}
/*??函?用??行?入的sql語句,并打印出查?結果*/
void query_sql(char* sql) {
MYSQL my_connection; /*?是一??据??接*/
int res; /*?行sql語句后的返回?志*/
MYSQL_RES *res_ptr; /*指向查??果的指?*/
MYSQL_FIELD *field; /*字段?构指?*/
MYSQL_ROW result_row; /*按行返回的查?信息*/
int row, column; /*查?返回的行?和列?*/
int i, j; /*只是控制循?的???量*/
/*初始化mysql?接my_connection*/
mysql_init(&my_connection);
/*?里就是用了mysql.h里的一?函?,用我?之前定?的那些宏建立mysql?接,并
返回一?值,返回不?空?明?接是成功的*/
if (mysql_real_connect(&my_connection, HOST, USERNAME, PASSWORD,
DATABASE, 0, NULL, CLIENT_FOUND_ROWS)) {/*?接成功*/
printf("?据?查?query_sql?接成功!\n");
/*?句?是?置查????utf8,??支持中文*/
mysql_query(&my_connection, "set names utf8");
/*下面?句?就是用mysql_query函???行我????入的sql語句,
??返回一?int值,如果?0,?明語句?行成功*/
res = mysql_query(&my_connection, sql);
if (res) { /*?在就代表?行失?了*/
printf("Error: mysql_query !\n");
/*不要忘了???接*/
mysql_close(&my_connection);
} else { /*?在就代表?行成功了*/
/*?查?的結果?res_ptr*/
res_ptr = mysql_store_result(&my_connection);
/*如果?果不?空,就把?果print*/
if (res_ptr) {
/*取得結果的行?和*/
column = mysql_num_fields(res_ptr);
row = mysql_num_rows(res_ptr) + 1;
printf("查?到 %lu 行 \n", row);
/*?出結果的字段名*/
for (i = 0; field = mysql_fetch_field(res_ptr); i++)
printf("%st", field->name);
printf("\n");
/*按行?出結果*/
for (i = 1; i < row; i++) {
result_row = mysql_fetch_row(res_ptr);
for (j = 0; j < column; j++)
printf("%st", result_row[j]);
printf("\n");
}
}
/*不要忘了???接*/
mysql_close(&my_connection);
}
}
}
int main(int argc, char *argv[]) {
/*??下向里面插入?据*/

       
 int i=1;
 for(int j=0;j<100;j++)

 {
  cout<<i<<endl;
  i++;
  Sleep(2000);
 
char *query;
char *exe = "insert into location (lat, lng) values(j,121)";
exe_sql(exe);
query="select * from location;";
query_sql(query);

  } 



/*??下查?*/

system("pause");

return 0;
}

我是要做出每格2秒會加1的輸出(輸出到mysql資料庫)-像是經緯度的樣子(0,121),(1,121),(2,121)
請問要如何改才可以正確顯示啊?

頁: [1]