[postgis-users] JDBC and PostGIS

Gustavo Henrique Sberze Ribas gribas at cpqd.com.br
Wed Jun 29 07:59:21 PDT 2005


Hi,

  I'm having some issues while trying to use PreparedStatements
to query a PostGIS database... I can't make it work :(
  
  I've added the geometric type entries as usual:

((org.postgresql.PGConnection)conn).addDataType("geometry",org.postgis.PGgeometry.class);
((org.postgresql.PGConnection)conn).addDataType("box3d",org.postgis.PGbox3d.class);

  And tried this simple insert query:
  
    Point p = new Point(-47.12, -22.374);
    p.setSrid(4326); 

    PreparedStatement ps = conn.prepareStatement("INSERT INTO jdbc_test(geom,id) VALUES(?,?)");
--> ps.setObject(1, p); // replaces first  "?" so geom = Point(-47.12, -22.374)
    ps.setInt(2, 1);    // replaces second "?" so id   = 1
    ps.executeUpdate();

  It crashes giving a PSQLException saying it couldn't find the appropriate type.
So I tried the following, with no sucess either:

    ps.setObject(1, p, java.sql.Types.OTHER);         // can't convert Point to Types.OTHER
    ps.setObject(1, p, conn.getSQLType("geometry"));  // can't convert Point to Types.OTHER
    ps.setObject(1, p, conn.getPGType("geometry"));   // Unsuported value: 72.190
    ps.setObject(1, p, Geometry.POINT); // ERROR: column "geom" is of type geometry but 
                                        // expression is of type character
    ps.setObject(1, p, p.type);         // ERROR: column "geom" is of type geometry but 
                                        // expression is of type character
    ps.setString(1, p.toString());      // ERROR: column "geom" is of type geometry but 
          ^^^^^^                        // expression is of type character

  
  Any thoughts ? I've attached a java program based on 
TestServer.java that implements this small test.


System Info:

  PostgreSQL 8.0.3 on i686-pc-mingw32, compiled by GCC gcc.exe (GCC) 3.4.2 (mingw-special)
  POSTGIS="1.0.1" GEOS="2.1.1" PROJ="Rel. 4.4.9, 29 Oct 2004" USE_STATS DBPROC="0.3.0" RELPROC="0.3.0"
  PGDrivers:  postgresql-8.0-311.jdbc3.jar (last stable release) 
              postgis_1_0_0.jar (came with windows installer)
  java version "1.5.0_03"
			  

Thanks,
Gustavo
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Test.java
Type: application/octet-stream
Size: 3680 bytes
Desc: Test.java
URL: <http://lists.osgeo.org/pipermail/postgis-users/attachments/20050629/003e6715/attachment.obj>


More information about the postgis-users mailing list