由于公司业务需求,需要把shape类型数据导入数据库。期间遇到了一些坑,在此记录一下。
json数据格式为:
{
"coordinates": [
[
[
[121.9803, 37.5592],
[121.9802, 37.5536],
[121.9694, 37.5536],
[121.9694, 37.5592],
[121.9803, 37.5592]
]
]
],
"type": "MultiPolygon"
}
需要的格式为:
MultiPolygon(((121.9803 37.5592,121.9802 37.5536,121.9694 37.5536,121.9694 37.5592,121.9803 37.5592)))
经过简单分析,可以取出坐标值,然后把"[","]"替换成"(",")",把逗号“,”替换为空格“ ”,再根据需要对字符串进行截取,然后就是把部分字符再替换为逗号。代码如下:
String coordinates=jsonObject.getString("coordinates")
.replace("[", "(").replace(",", " ").replace("]", ")");
String point=coordinates.substring(1, coordinates.length()-1).replace(") (", ",");
String tt="MultiPolygon"+point;
项目使用了ssm框架,xml中的插入语句如图。
shape为空间数据字段。使用varchar类型时,长度超过4000,无法插入数据库,会报错。直接在xml中把shape字段设置为jdbcType=CLOB也无法解决。解决方案:使用jdbc的方式进行插入。把shape字段由String类型转为CLOB类型
CLOB clob = oracle.sql.CLOB.createTemporary(c, false,oracle.sql.CLOB.DURATION_SESSION);
clob.putString(1, scope.getShape());
String sql="insert into t_scope(guid,is_save,acreage,create_user,project_name,scope_type,is_confirm,shape,geom)"
+ " values(?,?,?,?,?,?,?,SDO_GEOMETRY(?,4326),?)";
/**
*
* @author: Administrator
* @description: xxxx
* @data: 2018年11月2日 下午3:22:47
* @param sql sql语句
* @param scope 实体对象
* @param type 类型
* @return
*/
@SuppressWarnings("deprecation")
public Boolean jdbcAdd(String sql,Scope scope,int type) {
Connection c = null;
PreparedStatement p = null;
boolean bool = true;
try{
c = DBUtils.getConnection();
c.setAutoCommit(false);
CLOB clob = oracle.sql.CLOB.createTemporary(c, false,oracle.sql.CLOB.DURATION_SESSION);
CLOB clob1 = oracle.sql.CLOB.createTemporary(c, false,oracle.sql.CLOB.DURATION_SESSION);
clob.putString(1, scope.getShape());
clob1.putString(1, scope.getGeom());
p = c.prepareCall(sql);
if (type==3) {
//海域数据
p.setString(1, scope.getGuid());
p.setString(2, scope.getIsSave());
p.setString(3, scope.getAcreage());
p.setString(4, scope.getCreateUser());
p.setString(5, scope.getProjectName());
p.setString(6, scope.getScopeType());
p.setString(7, scope.getSubClass());
p.setClob(8, clob);
p.setClob(9, clob1);
}else if (type==2){
//确权水产数据
p.setString(1, scope.getGuid());
p.setString(2, scope.getIsSave());
p.setString(3, scope.getAcreage());
p.setString(4, scope.getCreateUser());
p.setString(5, scope.getProjectName());
p.setString(6, scope.getScopeType());
p.setString(7, scope.getIsConfirm());
p.setString(8, scope.getBreedType());
p.setClob(9, clob);
p.setClob(10, clob1);
}else if (type==1){
//未确权水产数据
p.setString(1, scope.getGuid());
p.setString(2, scope.getIsSave());
p.setString(3, scope.getAcreage());
p.setString(4, scope.getCreateUser());
p.setString(5, scope.getProjectName());
p.setString(6, scope.getScopeType());
p.setString(7, scope.getIsConfirm());
p.setClob(8, clob);
p.setClob(9, clob1);
}
bool= p.execute();
p.close();
c.commit();
c.close();
}catch(Exception e){
try {
c.rollback();
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
e.printStackTrace();
}finally{
if(p!=null){
try{
p.close();
}catch(Exception e){
e.printStackTrace();
}
}
if(c!=null){
try{
c.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
return bool;
}