后端JSP文件里Java语言如果操作Oracle Spatial提供的JGeometry对象

记个流水账,记录下这个困扰自己多时的问题最后怎么解决的。当时觉得该尝试的都尝试了,纳闷到底怎样能解决的,最后还是解决了~

正确的代码如下:

<%@ 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");

然后由于用到JGeometry对象,JGeometry对象是在sdoapi.jar中提供的。其中需要用到sdoapi.jar和sdoutl.jar。所以需要在头信息的import里导入相应包:oracle.spatial.geometry.*,oracle.spatial.util.*,oracle.sql.*

import="java.sql.*,java.lang.*,oracle.spatial.geometry.*,oracle.spatial.util.*,oracle.sql.*"

我之前导入的是oracle.spatial.geometry.*,oracle.jdbc.driver.*,oracle.sql.STRUCT; 没有导入oracle.spatial.util.*,可能导致JGeometry类型报错。

之前都是在浏览器加载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);


郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。