記個流水賬,記錄下這個困擾自己多時的問題最後怎麼解決的。當時覺得該嘗試的都嘗試了,納悶到底怎樣能解決的,最後還是解決了~
正確的代碼如下:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,java.lang.*,oracle.spatial.geometry.*,oracle.spatial.util.*,oracle.sql.*" errorPage="" %> <!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 http-equiv="Content-Type" content="text/html; charset=gb2312" /> <title>no title text</title> </head> <body> <% response.addHeader("Access-Control-Allow-Origin", "http://192.168.14.216:8888");//oracle.spatial.geometry.*,oracle.jdbc.driver.*,oracle.sql.STRUCT; //out.print("OK!"); String SQLStatement =""; String sp =request.getParameter("sp"); //out.print(sp); Class.forName("oracle.jdbc.driver.OracleDriver"); //加載JDBC驅動程序 //建立連接 String URL = "jdbc:oracle:thin:@localhost:1521:cad"; Connection conn = DriverManager.getConnection(URL,"cadadmin","cad"); /*if(conn.isClosed()) out.print("Failed to connect to the database!"); else out.print("Succeed connecting to the database!");*/ Statement stmt=conn.createStatement(); //求sp房間所對應的對象集合 //SQLStatement = "select rm.x,rm.y,rm.z from table(select geom from room where idc = "+sp+") rm"; //SQLStatement = "select mo.x,mo.y,mo.z,mo.room_id from table(select NavWithPosIndoor(position4indoor("+pos1id+","+pos1x+","+pos1y+","+pos1z+"),position4indoor("+pos2id+","+pos2x+","+pos2y+","+pos2z+"),ind.indoor) from inSpace ind) mo"; SQLStatement = "select geom from room where idc = "+sp; ResultSet res_room=stmt.executeQuery(SQLStatement); if(res_room.next()){ STRUCT dbObject=(STRUCT)res_room.getObject(1); JGeometry geom=JGeometry.load(dbObject); int plen=geom.getNumPoints();//點個數 //out.print("plen"+plen); double rmzb[]=new double[plen*2]; rmzb=geom.getOrdinatesArray(); for(int i=0;i<plen*2;++i) { out.print(rmzb[i]+","); } //String rmid=res_room.getString(1); //out.print("room:"+rmid); //String rmx=res_room.getString(2); //String rmy=res_room.getString(3); //String rmz=res_room.getString(4); //out.print("room:"+rmid+" "+rmx+" ");//+rmy+" "+rmz+" "); } out.print("1"); stmt.close(); conn.close(); %> </body> </html>其中,由於地圖是在8888端口,而該jsp文件是在tomcat文件夾下webapps文件夾下新建的項目文件夾下,在8080端口。在chrome浏覽器運行,需要如下代碼:
response.addHeader("Access-Control-Allow-Origin", "http://192.168.14.216:8888");
import="java.sql.*,java.lang.*,oracle.spatial.geometry.*,oracle.spatial.util.*,oracle.sql.*"
之前都是在浏覽器加載jsp頁面時報錯JGeometry是未定義類型。後來沒報這個錯了,但還是有問題,一直以為還是這個問題,以為JGeometry不可用。(因為之前報JGeometry錯的時候,也是有時報錯有時不報錯。)
然後程序裡,通過select geom 去除sdo_geometry對象,再進行getObject和load。其中getObject()中的索引是指總共的項索引,不是指Object的索引。比如第一個屬性是String,第二個屬性是sdo_geometry。則取值依次為getString(1)和getObject(2)。我之前是select *了,然後getObject(1),所以是有問題的。(select * 出來的第一個是String,第二個是sdo_geometry)
SQLStatement = "select geom from room where idc = "+sp; ResultSet res_room=stmt.executeQuery(SQLStatement); if(res_room.next()){ STRUCT dbObject=(STRUCT)res_room.getObject(1); JGeometry geom=JGeometry.load(dbObject);