简体   繁体   中英

How to retrieve SDO_GEOMETRY from database using JDBC?

I am new to Java.I need to retrieve SDO_GEOMETRY from database and convert it into string using jdbc.If any one has sample or idea please share with me.

Help would be appreciated.

Best Regards,
Sanjay.

Have a look into JGeometry class. There is an example to call the spatial type:

     /// reading a geometry from database
     ResultSet rs = statement.executeQuery("SELECT geometry FROM states where name='Florida'");
     STRUCT st = (oracle.sql.STRUCT) rs.getObject(1);
     //convert STRUCT into geometry
     JGeometry j_geom = JGeometry.load(st);

     // ... manipulate the geometry or create a new JGeometry ...

     /// writing a geometry back to database
     PreparedStatement ps = connection.prepareStatement("UPDATE states set geometry=? where name='Florida'");
     //convert JGeometry instance to DB STRUCT
     STRUCT obj = JGeometry.store(j_geom, connection);
     ps.setObject(1, obj);
     ps.execute();

If the Oracle Spatial API is not available for you, eg because you use OpenJDK (cf. Oracle FAQs ), there is also a more rudimentary way to access an SDO_GEOMETRY object from a ResultSet r :

import java.sql.Struct;
import java.sql.Array;

//...

// access SDO_GEOMETRY of row 1
Object[] geometryArray = ((Struct) r.getObject(1)).getAttributes();
// read its components
int gtype = ((Integer) geometryArray[0]).intValue();
int srid = ((Integer) geometryArray[1]).intValue();
Double[] pointArray = ((Double[]) ((Array) geometryArray[2]).getArray());
Integer[] elemInfoArray = ((Integer[]) ((Array) geometryArray[3]).getArray());
Double[] ordinateArray = ((Double[]) ((Array) geometryArray[4]).getArray());

You can also unwrap the number types by a simple loop, eg,

double[] ordinates = new double[ordinateArray.length];
for (int i = 0; i < ordinates.length; i++)
     ordinates[i] = ordinateArray[i].doubleValue();

or using an equivalent more convenient method (cf. StackOverflow: How do I convert Double[] to double[]? ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM