package com.gradle.tool.shpDataInsert;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.ShapefileDataStoreFactory;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.opengis.feature.simple.SimpleFeature;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.SQLException;
/**
* @autor xujing
* @date 2019-10-17 16:11
*/
public class ShpToSql {
public static void main(String[] args) throws SQLException, IOException {
Connection conn = null;
conn = DBUtil.getConn();
ShapefileDataStoreFactory dataStoreFactory = new ShapefileDataStoreFactory();
String path = ShpToSql.filepath;
ShapefileDataStore sds = (ShapefileDataStore)dataStoreFactory.createDataStore(new File(path).toURI().toURL());
sds.setCharset(Charset.forName("GBK"));
SimpleFeatureSource featureSource = sds.getFeatureSource();
SimpleFeatureIterator itertor = featureSource.getFeatures().features();
while(itertor.hasNext()){
SimpleFeature feature = itertor.next();
// System.out.println(feature.getIdentifier().toString().substring(6));
StringBuffer sql = new StringBuffer();
//sql.append("insert into routinemonitor.t_bas_grid_layer(the_geom,gd_code," +
// //"fun_area," +
// "region)values(");
sql.append("insert into routinemonitor.t_bas_fun_layer(the_geom,type,limits,region)values(");
sql.append(
// "'"+ feature.getIdentifier().toString().substring(6) +"',"
"'"+ feature.getAttributes().get(0).toString()+"',"
+"'"+feature.getAttributes().get(1).toString()+"',"
+"'"+feature.getAttributes().get(2).toString()+"',"
+"'"+"qu"+"'"
// +"'"+feature.getAttributes().get(4).toString()+"'"
//+"'"+ "qu"+"'"
);
sql.append(");");
System.out.println(sql.toString());
// UserLogUtils.writeToLog(sql.toString());
// conn.createStatement().execute(sql.toString());
}
itertor.close();
}
private static String filepath="C:\\Users\\wang\\Desktop\\gridPoint\\gridPoint_Enve.shp";
}