簡體   English   中英

如何使用Java JDBC PreparedStatement占位符將GeoJSON多邊形插入PostGIS?

[英]How can you insert GeoJSON polygon into PostGIS using Java JDBC PreparedStatement placeholder?

如果您使用的是JavaScript庫“ Leaflet”及其插件“ Draw”,則可以檢索geojson,如下所示:

{
    "type" : "Polygon",
    "coordinates" : [ [ [ 13.95938491821289, 53.41329518072117 ], [ 14.955865859985348, 54.412618223375205 ], [ 13.9606294631958, 54.4120067663998 ], [ 13.95938491821289, 53.41329518072117 ] ] ]
}

如果您如上所述將json發送到網絡服務器(帶有Java代碼,例如Play Framework),那么我想知道在不使用字符串串聯創建sql字符串的情況下,將其插入或更新到postgis數據庫中的最簡單方法。 實際上,它可以使用上述結構創建SQL字符串,但是您要避免字符串串聯,這是我關於如何改為使用PreparedStatement占位符的問題的原因。 例如,可以從pgAdmin直接執行以下SQL代碼(也可以通過JDBC代碼):

UPDATE polygons
SET 
    geom = ST_SetSRID(ST_GeomFromGeoJSON
    ('{
            "type" : "Polygon",
            "coordinates" : [ [ [ 13.95938491821289, 53.41329518072117 ], [ 14.955865859985348, 54.412618223375205 ], [ 13.9606294631958, 54.4120067663998 ], [ 13.95938491821289, 53.41329518072117 ] ] ]
    }'), 26918)  
WHERE polygonid = 1;

但是,正如大多數開發人員所了解的那樣,我們應該避免String串聯,而應使用占位符,例如,如果使用PreparedStatement(或者如果使用Spring Framework,則為JdbcTemplate)。

下面是一些代碼,說明了什么在起作用,什么在不起作用。

我正在使用的PostGIS數據庫表是通過以下語句創建的:

CREATE TABLE polygons (
  polygonid serial NOT NULL,
  geom geometry(Polygon,26918),
  CONSTRAINT polygon_pkey PRIMARY KEY (polygonid)
);

在我的Maven文件pom.xml中,我正在使用此依賴項:

<dependency>
    <groupId>net.postgis</groupId>
    <artifactId>postgis-jdbc</artifactId>
    <version>2.2.0</version>
</dependency>

Java代碼:

import java.sql.*;
import org.postgis.PGgeometry;
...
// For verbosity reasons, I am below not including 
// any try/catch/throw statements or any closings of connections/statements/recordsets

String url = "jdbc:postgresql://localhost:5432/nameOfYourDatabase";
Connection connection = DriverManager.getConnection(url, "postgres", "[YOUR_PASSWORD]");    

// First below is some SQL query code which works and thus 
// proves that the setup with jar files and database connection indeed works:    
long maxPolygonId  = 100L;
PreparedStatement ps = connection.prepareStatement(
    "SELECT polygonid , geom FROM polygons WHERE polygonid < ?"
);
ps.setLong(1, maxPolygonId);
ResultSet rs = ps.executeQuery();
while(rs.next()) {
    long polygonId = rs.getLong(1);
    // Actually it works to use PGgeometry as below WITHOUT first having to do 
    // "((org.postgresql.PGConnection)conn).addDataType("geometry",Class.forName("org.postgis.PGgeometry"));" 
    // as currently documented at http://postgis.net/docs/ch06.html#idp48666800
    PGgeometry geom = (PGgeometry)rs.getObject(2); 
    System.out.println("polygonId: " + polygonId);
    System.out.println("geom: " + geom); // e.g. "geom: SRID=26918;POLYGON((17.9661226272583 59.41375375704178, ... ,17.9661226272583 59.41375375704178))"
}        

// Below is some code that also indeed works fine but it uses string 
// concatenation for the json string instead of placeholder which I want to use instead   
String geoJson = 
    "   {   " +
    "       \"type\" : \"Polygon\",  " +
    "       \"coordinates\" : [ [ [ 13.95938491821289, 53.41329518072117 ], [ 14.955865859985348, 54.412618223375205 ], [ 13.9606294631958, 54.4120067663998 ], [ 13.95938491821289, 53.41329518072117 ] ] ]  " +
    "   }  ";                   
String sqlUpdatingWithGeoJson = 
        "  UPDATE polygons  " +
        "   SET  " +
        "       geom = ST_SetSRID(ST_GeomFromGeoJSON  " +
        "       ('  " +
                    geoJson + 
        "       '), 26918)  " +
        "   WHERE polygonid = ?  ";
long idForPolygonToUpdate = 1L;
ps = connection.prepareStatement(sqlUpdatingWithGeoJson);
ps.setLong(1, idForPolygonToUpdate);
ps.executeUpdate();

// Below is some code which does NOT work when I am trying 
// to use placeholder instead of string concatenation as above 
String sqlUpdatingWithGeoJsonAsPlaceHolder = 
        "  UPDATE polygons  " +
        "   SET  " +
        "       geom = ST_SetSRID(ST_GeomFromGeoJSON  " +
        "       ('  " +
                    " ? " + // NOTE that this line is the difference with the sql update string above ! 
        "       '), 26918)  " +
        "   WHERE polygonid = ?  ";         

ps = connection.prepareStatement(sqlUpdatingWithGeoJsonAsPlaceHolder);
ps.setString(1, geoJson);
ps.setLong(2, idForPolygonToUpdate);
// The above row results in the exception below
// org.postgresql.util.PSQLException: The column index is out of range: 2, number of columns: 1.            
ps.executeUpdate();               


// Below I instead tried  to replace the whole expression as a string to be set 
// through the placeholder but it does NOT work neither  
String geomValue =
        "       ST_SetSRID(ST_GeomFromGeoJSON  " +
        "       ('  " +
                    geoJson + 
        "       '), 26918)  ";          
String sqlUpdatingWithPostGisStatementForPlaceHolder = 
        "  UPDATE polygons  " +
        "   SET  " +
        "       geom = ?  " +
        "   WHERE polygonid = ?  ";
ps = connection.prepareStatement(sqlUpdatingWithPostGisStatementForPlaceHolder);
ps.setString(1, geomValue); // I have also tried setObject instead of setString here
ps.setLong(2, idForPolygonToUpdate);
// The above row also results in the same exception as previous above i.e. below exception:
// org.postgresql.util.PSQLException: The column index is out of range: 2, number of columns: 1.    
ps.executeUpdate();

也許解決方案可能是將json字符串轉換為PostGIS / PostgreSQL JDBC驅動程序可以理解的Java對象,然后使用方法“ prepareStatement.setObject”,但是該Java對象應使用什么類型以及如何使用您輕松將json字符串轉換為此類對象?

String sqlUpdatingWithGeoJsonAsPlaceHolder = 
        "  UPDATE polygons  " +
        "   SET  " +
        "       geom = ST_SetSRID(ST_GeomFromGeoJSON(?), 26918)  " +
        "   WHERE polygonid = ?  ";         

ps = connection.prepareStatement(sqlUpdatingWithGeoJsonAsPlaceHolder);
ps.setString(1, geoJson);
ps.setLong(2, idForPolygonToUpdate);      
ps.executeUpdate();   

這應該可行:您將geojson作為字符串傳遞。 函數ST_GeomFromGeoJSON將字符串作為第一個參數,因此可以准備和執行該語句。

您收到該錯誤是因為您引用了問號。 這樣,問號不會被解釋為占位符!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM