2013-10-16 45 views
5

我試圖將幾何對象存儲到我的postgist數據庫中,該數據庫具有帶幾何列的表。我從具有幾何列的另一個表中獲取了幾何值,並且打印了之前獲得的值,這沒關係。要存儲幾何值我用下面的函數:如何使用java在Postgis數據庫中存儲幾何點

static void insertaGeometria(Geometry geom, int idInstalacion) throws ClassNotFoundException, SQLException{ 

     Connection congeom = conectarPGA(); 

     String geomsql ="INSERT INTO georrepositorio.geometria(id, point) VALUES (?,?)"; 
     PreparedStatement psSE= congeom.prepareStatement(geomsql); 
     psSE.setInt(1, idInstalacion); 
     psSE.setObject(2, geom);  

     psSE.execute(); 
     psSE.close(); 
     congeom.close();  
    } 

但我總是得到這樣的錯誤:

org.postgresql.util.PSQLException: Can't infer the SQL type to use for an instance of org.postgis.Point. Use setObject() with an explicit Types value to specify the type to use.

難道有人知道如何保存呢? !='。(

在此先感謝

回答

4

See the manual Java客戶端從這個我看到了兩個想法試着用的PGgeometry代替Geometry類型geom然後,幾何類型添加到連接congeom

((org.postgresql.PGConnection)congeom).addDataType("geometry",Class.forName("org.postgis.PGgeometry")); 
+0

什麼情況下做jpa –

+0

@ bob-cac不確定(我不使用JPA);看看[如果這有幫助](http://stackoverflow.com/q/2387170/327026) –

2

要我的經驗,我已經成功使用這種表達添加點(注意該請求是通過我自己的類迭代的點):

java.sql.Connection conpg; 

    try { 
/* 
* Load the JDBC driver and establish a connection. 
*/ 

     Class.forName("org.postgresql.Driver"); 
     String url = "jdbc:postgresql://localhost:5432/postgis_22_sample"; 
     conpg = DriverManager.getConnection(url, "postgres", "nypassw"); 
/* 
* Add the geometry types to the connection. Note that you 
* must cast the connection to the pgsql-specific connection 
* implementation before calling the addDataType() method. 
*/ 
     ((org.postgresql.PGConnection) conpg).addDataType("geometry", Class.forName("org.postgis.PGgeometry")); 
     //((org.postgresql.PGConnection)conpg).addDataType("point",Class.forName("org.postgis.Point")); 
/* 
* Create a statement and execute a select query. 
*/ 
     conpg.setAutoCommit(false); 

     for (Point p : points) { 

      org.postgis.Point pointToAdd = new org.postgis.Point(); 
      pointToAdd.setX(p.getLongitude()); 
      pointToAdd.setY(p.getLatitude()); 

      //Statement s = conn.createStatement(); 
      //String geomsql = ; 
      PreparedStatement psSE = conpg.prepareStatement("INSERT INTO public.\"poi-point\" (name,geom,leisure) VALUES (?,?,?)"); 
      psSE.setString(1, p.getDescription()); 
      psSE.setObject(2, new org.postgis.PGgeometry(pointToAdd)); 
      psSE.setString(3, "marina"); 

      psSE.execute(); 
      //ResultSet r = s.executeQuery("select geom,id from geomtable"); 
      //while (r.next()) { 
    /* 
    * Retrieve the geometry as an object then cast it to the geometry type. 
    * Print things out. 
    */ 
      // PGgeometry geom = (PGgeometry) r.getObject(1); 
      // int id = r.getInt(2); 
      // System.out.println("Row " + id + ":"); 
      // System.out.println(geom.toString()); 
      //} 
      //s.close(); 
     } 
     conpg.commit(); 
     conpg.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

Maven的依賴關係,以它的工作是

<dependency> 
     <groupId>net.postgis</groupId> 
     <artifactId>postgis-jdbc</artifactId> 
     <version>2.2.0</version> 
     <exclusions> 
      <exclusion> 
       <!-- NOTE: Version 4.2 has bundled slf4j --> 
       <groupId>org.slf4j</groupId> 
       <artifactId>slf4j-api</artifactId> 
      </exclusion> 
      <exclusion> 
       <!-- NOTE: Version 4.2 has bundled slf4j --> 
       <groupId>ch.qos.logback</groupId> 
       <artifactId>logback-classic</artifactId> 
      </exclusion> 
      <exclusion> 
       <!-- NOTE: Version 4.2 has bundled slf4j --> 
       <groupId>ch.qos.logback</groupId> 
       <artifactId>logback-core</artifactId> 
      </exclusion> 
     </exclusions> 

注意,你不nessesarilly需要排除的依賴(這是需要我自己的兼容性)

相關問題