使用geotools的将shp数据写入mysql时报Could not initialize class org.hsqldb.lib.FrameworkLoggerwenti错误
报错问题截图如下:
代码:
package xyz.guqing.geotools;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.FeatureWriter;
import org.geotools.data.Transaction;
import org.geotools.data.mysql.MySQLDataStoreFactory;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureCollection;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.jdbc.JDBCDataStore;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
public class ShpFileOut2Mysql {
public static void main(String[] args) {
JDBCDataStore connnection2mysql = ShpFileOut2Mysql.connnection2mysql("localhost", "geo_metadata", 3306, "root", "root");
SimpleFeatureSource featureSource = readSHP("县道_polyline.shp");
// 写入mysql
JDBCDataStore ds = createTable(connnection2mysql, featureSource);
writeShp2Mysql(ds, featureSource);
}
public static SimpleFeatureSource readSHP(String shpfile) {
SimpleFeatureSource featureSource = null;
try {
File file = new File(shpfile);
ShapefileDataStore shpDataStore = null;
shpDataStore = new ShapefileDataStore(file.toURL());
// 设置编码
Charset charset = Charset.forName("GBK");// UTF-8
shpDataStore.setCharset(charset);
String tableName = shpDataStore.getTypeNames()[0];
featureSource = shpDataStore.getFeatureSource(tableName);
} catch (Exception e) {
e.printStackTrace();
}
return featureSource;
}
public static JDBCDataStore connnection2mysql(String host, String dataBase, int port, String userName, String pwd) {
JDBCDataStore ds = null;
DataStore dataStore = null;
// 连接数据库参数
Map<String,Object> params = new HashMap<>();
params.put(MySQLDataStoreFactory.DBTYPE.key, "mysql");
params.put(MySQLDataStoreFactory.HOST.key, host);
params.put(MySQLDataStoreFactory.PORT.key, port);
params.put(MySQLDataStoreFactory.DATABASE.key, dataBase);
params.put(MySQLDataStoreFactory.USER.key, userName);
params.put(MySQLDataStoreFactory.PASSWD.key, pwd);
try {
dataStore = DataStoreFinder.getDataStore(params);
if (dataStore != null) {
ds = (JDBCDataStore) dataStore;
System.out.println(dataBase + "连接成功");
} else {
System.out.println(dataBase + "连接失败");
}
} catch (IOException e) {
e.printStackTrace();
}
return ds;
}
public static JDBCDataStore createTable(JDBCDataStore ds, SimpleFeatureSource featureSource) {
SimpleFeatureType schema = featureSource.getSchema();
try {
// 创建数据表
ds.createSchema(schema);
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("创建数据表失败");
e.printStackTrace();
}
return ds;
}
public static void writeShp2Mysql(JDBCDataStore ds, SimpleFeatureSource featureSource) {
SimpleFeatureType schema = featureSource.getSchema();
//开始写入数据
try {
FeatureWriter<SimpleFeatureType, SimpleFeature> writer = ds
.getFeatureWriter(schema.getTypeName().toLowerCase(), Transaction.AUTO_COMMIT);
SimpleFeatureCollection featureCollection = featureSource.getFeatures();
SimpleFeatureIterator features = featureCollection.features();
while (features.hasNext()) {
writer.hasNext();
SimpleFeature next = writer.next();
SimpleFeature feature = features.next();
for (int i = 0; i < feature.getAttributeCount(); i++) {
next.setAttribute(i, feature.getAttribute(i));
}
writer.write();
}
writer.close();
ds.dispose();
System.out.println("导入成功");
} catch (IOException e) {
e.printStackTrace();
}
}
}
还可能报postgresql连接失败
的错误,但是并没有使用到postgresql
所以只有可能是引用的jar缺少配置导致的错误。
上面两种问题的解决方法都是移除以下jar包的依赖:
至于geotools
的我使用的是18版本
下载地址:
// TODO 待补充