【问题标题】:How to store geometry Point in Postgis database using java如何使用java在Postgis数据库中存储几何点
【发布时间】:2013-10-24 05:36:54
【问题描述】:

我正在尝试将几何对象存储到我的 postgist 数据库中,该数据库有一个带有几何列的表。我从另一个带有几何列的表中获取了几何值,然后打印了之前得到的值,没关系。为了存储几何值,我使用下一个函数:

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:无法推断要用于的 SQL 类型 org.postgis.Point 的一个实例。将 setObject() 与显式一起使用 类型值以指定要使用的类型。

有人知道怎么保存吗? ='(

提前致谢!

【问题讨论】:

    标签: java postgresql postgis


    【解决方案1】:

    根据我的经验,我已经设法使用这样的表达式添加点(注意迭代请求的点是我自己的类):

        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>
    

    请注意,您不一定需要排除依赖项(我自己的兼容性需要它)

    【讨论】:

      【解决方案2】:

      See the manual 用于 Java 客户端。从这里我看到两个想法。尝试使用PGgeometry 而不是Geometry 类型来表示geom。然后,将几何类型添加到连接congeom

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

      【讨论】:

      • 遇到jpa怎么办
      • @bob-cac 不确定(我不使用 JPA);见if this helps
      猜你喜欢
      • 2021-05-05
      • 1970-01-01
      • 2020-09-29
      • 1970-01-01
      • 2015-12-07
      • 2022-01-24
      • 1970-01-01
      • 2020-12-21
      • 2022-10-05
      相关资源
      最近更新 更多