使用geotools的将shp数据写入mysql时报Could not initialize class org.hsqldb.lib.FrameworkLoggerwenti错误

在使用geotools将shp数据导入MySQL时遇到'Could not initialize class org.hsqldb.lib.FrameworkLogger'错误。问题可能由未配置的jar依赖引起。解决方案是移除特定的hsqldb jar包依赖,确保不使用未配置的库。提供了解决此问题的下载链接。
摘要由CSDN通过智能技术生成

报错问题截图如下:
在这里插入图片描述
代码:

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版本
下载地址:

https://download.csdn.net/download/qq_22174779/11162767
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值