使用Java(jdbc)在mysql中插入地理空间数据类型(mutipolygon) [英] insert geospatial datatype( mutipolygon) in mysql with java( jdbc)
问题描述
我正在使用geotools库提取位置信息.这样我得到一个类型的对象
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
我现在想通过jdbc连接将该字段存储在我的mysql表中.
当我直接尝试将其插入为
pstmtInsert.setObject(4, geoobject)
我收到此错误
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
推荐答案
MySql不知道如何存储您的GEO对象,或者他的大小是多少.您不应该按照尝试的方式存储对象.
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.
PreparedStatement#setObject()文档说:
JDBC规范指定了从Java对象类型到SQL类型的标准映射.给定的参数在发送到数据库之前将被转换为相应的SQL类型. [...] 如果存在歧义,例如,如果对象属于实现了以上所指接口之一的类,则此方法将引发异常.
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.
这篇关于使用Java(jdbc)在mysql中插入地理空间数据类型(mutipolygon)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!