insert geospatial datatype( mutipolygon) in mysql with java( jdbc)

半腔热情 提交于 2019-11-29 17:37:11

MySql can't know how to store your GEO object, or what is his size. You should not store the object the way you're trying.

The PreparedStatement#setObject() documentation says :

The JDBC specification specifies a standard mapping from Java Object types to SQL types. The given argument will be converted to the corresponding SQL type before being sent to the database. [...] This method throws an exception if there is an ambiguity, for example, if the object is of a class implementing more than one of the interfaces named above.

Answer

  1. You need to convert the geometry object you have to well known text. You'll find information on how to do that in the vividsolutions API documentation.

    geoobject.toText();
    
  2. Insert / Update the data using the mysql GeomFromText method.

    INSERT INTO geom VALUES (GeomFromText(@g));
    

It can be binary as well, e.g.

PreparedStatement preparedStatement = connection.prepareStatement
("INSERT INTO table (point, polygon) VALUES (PointFromWKB(?), GeomFromWKB(?))");

WKBWriter writer = new WKBWriter();

preparedStatement.setBytes(1, writer.write(point));
preparedStatement.setBytes(2, writer.write(polygon));

preparedStatement.executeUpdate();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!