insert geospatial datatype( mutipolygon) in mysql

2019-01-29 08:22发布

I am using geotools library to extract the location information. With that I am getting an object of type

class com.vividsolutions.jts.geom.MultiPolygon

I now want to store this field in my mysql table with a jdbc connection . When I directly try to insert it as pstmtInsert.setObject(4, geoobject) I am getting this error

Exception in thread "main" com.mysql.jdbc.MysqlDataTruncation: Data truncation: Cannot get geometry object from data you send to the GEOMETRY field

3条回答
戒情不戒烟
2楼-- · 2019-01-29 08:25

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();
查看更多
疯言疯语
3楼-- · 2019-01-29 08:26

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));
    
查看更多
神经病院院长
4楼-- · 2019-01-29 08:36

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.

查看更多
登录 后发表回答