[原创]Oracle Spatial新驱动的查询记录实例

[原创]Oracle Spatial新驱动的查询记录实例

--sunfruit
    Oracle的空间数据库的操作驱动更新了,新的驱动适用于Oracle8.0或以上,新驱动在对数据库的操作上面和原有的驱动差别比较大,不过有一点:使用变得简单了
    建立空间数据库和建立空间索引的步骤就略过了,那些网上有很多例子,而且实现方式上面也没有变化,下面列出查询空间数据库记录的代码:

import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.spatial.geometry.JGeometry;
import java.sql.PreparedStatement;
import oracle.sql.STRUCT;
import java.sql.Connection;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2006</p>
 *
 * <p>Company: </p>
 *
 * @author sunfruit
 * @version 1.0
 */
public class SdoSelect {
  public SdoSelect() {
  }
  public static void main(String[] args) {
    String driver = "oracle.jdbc.driver.OracleDriver";
    String url = "jdbc:oracle:thin:@172.16.75.200:1521:starmap";
    String uid = "hmbst";
    String psw = "hmbst";
    Connection conn = null;
    PreparedStatement ps=null;
    try {
      Class.forName(driver);
      conn = DriverManager.getConnection(url, uid, psw);

      JGeometry jGeometry=new JGeometry(41884696,14377039,42884696,14477039,0);
      STRUCT obj =jGeometry.store(jGeometry,conn);
      String sql = "SELECT * FROM POISDO p WHERE sdo_filter(p.gshape,?,'querytype=window')='TRUE'";
      ps = conn.prepareStatement(sql,ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
      ps.clearParameters();
      ps.setObject(1, obj);
      // 插入点地物记录
      ResultSet rs=ps.executeQuery();
      while(rs.next())
      {
        STRUCT st = (oracle.sql.STRUCT)rs.getObject("gshape");
        JGeometry j_geom = JGeometry.load(st);
        double[] dou=j_geom.getPoint();
        String buff="";
        for(int i=0;i<dou.length;i++)
        {
          buff=buff+String.valueOf((int)dou[i])+"   ";
        }
        System.out.println(buff);
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    finally
    {
      if(conn!=null)
      {
        try {
          conn.close();
        }
        catch (SQLException ex) {
          ex.printStackTrace();
        }
      }
      if(ps!=null)
      {
        try {
          ps.close();
        }
        catch (SQLException ex) {
          ex.printStackTrace();
        }
      }

    }
  }

}

表POISDO的结构如下
create table poi (
  id INTEGER,
  gname VARCHAR2(256),
  gshape MDSYS.SDO_GEOMETRY);

你可能感兴趣的:([原创]Oracle Spatial新驱动的查询记录实例)