[Solved] Error while importing shapefile into Oracle

gizmogaurav Asks: Error while importing shapefile into Oracle
I am trying to import shapefile into Oracle db using GeoTools, but when I am trying to create a new schema, it will giving me following error.

Aug 01, 2021 8:24:13 PM org.geotools.jdbc.JDBCDataStore getMapping WARNING: No mapping for org.locationtech.jts.geom.MultiPolygon Aug 01, 2021 8:24:13 PM org.geotools.jdbc.JDBCDataStore getSQLTypeNames WARNING: Fetching fields from Database Exception in thread “main” java.lang.NoClassDefFoundError: org/geotools/factory/Hints

I am using following jars for this.

Code:
gt-jdbc-oracle-2.6.1
ojdbc8-12.2.0.1

Not able to found gt-epsg-oracle jar not sure if this is happening because of this jar.

Below is the code which I am using:

Code:
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.geotools.data.DataStore;
import org.geotools.data.DataStoreFinder;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.Transaction;
import org.geotools.data.collection.ListFeatureCollection;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.simple.SimpleFeatureIterator;
import org.geotools.data.simple.SimpleFeatureSource;
import org.geotools.data.simple.SimpleFeatureStore;
import org.geotools.feature.FeatureCollection;
import org.geotools.jdbc.JDBCDataStoreFactory;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.filter.identity.FeatureId;

public class WriteToDatabase {
    private static DataStore dataStore;

    public WriteToDatabase(Map<String, Object> params) throws IOException {

        dataStore = DataStoreFinder.getDataStore(params);
        if(dataStore != null && dataStore.getTypeNames() != null)
            System.out.println("Oracle connected");
    }

    public boolean isReady() {
        return dataStore != null;
    }

    public boolean writeFeatures(FeatureCollection<SimpleFeatureType, SimpleFeature> features) {

        if (dataStore == null) {
            throw new IllegalStateException("Datastore can not be null when writing");
        }
        SimpleFeatureType schema = features.getSchema();

        try {

            /*
             * Write the features
             */
            Transaction transaction = new DefaultTransaction("create");

            String[] typeNames = dataStore.getTypeNames();
            // first check if we need to create table
            boolean exists = false;
            String schemaName = schema.getName().getLocalPart();
            for (String name : typeNames) {

                if (schemaName.equalsIgnoreCase(name)) {
                    exists = true;
                }
            }
            if (!exists) {
                **dataStore.createSchema(schema);**
            }
            // we should probably check the schema matches the existing table.
            // but we don't
            SimpleFeatureSource featureSource = dataStore.getFeatureSource(schema.getName().getLocalPart());
            if (featureSource instanceof SimpleFeatureStore) {
                SimpleFeatureStore featureStore = (SimpleFeatureStore) featureSource;

                featureStore.setTransaction(transaction);
                try {
                    List<FeatureId> ids = featureStore.addFeatures(features);
                    transaction.commit();
                } catch (Exception problem) {
                    problem.printStackTrace();
                    transaction.rollback();
                } finally {
                    transaction.close();
                }
                dataStore.dispose();
                return true;
            } else {
                dataStore.dispose();
                System.err.println("Database not writable");
                return false;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String args[]) throws IOException {
        File file;
        String path = "D:\shppoc.shp";
        ShapefileDataStore shp = new ShapefileDataStore(new File(path).toURL());

        shp.setCharset(Charset.forName("UTF-8"));
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(JDBCDataStoreFactory.USER.key, "GEO_LOAD");
        params.put(JDBCDataStoreFactory.PASSWD.key, "test");
        params.put(JDBCDataStoreFactory.HOST.key, "test");
        params.put(JDBCDataStoreFactory.PORT.key, "1521");
        params.put(JDBCDataStoreFactory.DATABASE.key, "/test1");
        params.put(JDBCDataStoreFactory.DBTYPE.key, "oracle");
        // create some random features and write them out;
        List<SimpleFeature> feats = new ArrayList<SimpleFeature>();

        SimpleFeatureIterator fi = shp.getFeatureSource().getFeatures().features();
        SimpleFeatureType sourceSchema = shp.getSchema();

        while (fi.hasNext()) {
            SimpleFeature source = (SimpleFeature) fi.next();
            feats.add(source);
        }

        WriteToDatabase writer = new WriteToDatabase(params);
        System.out.println("oracle.getTypeNames()"+dataStore.getTypeNames());
        String typeName = shp.getTypeNames()[0].toUpperCase();

        System.out.println("shp.getTypeNames()[0]===="+shp.getTypeNames()[0]);

        FeatureCollection<SimpleFeatureType, SimpleFeature> features = new ListFeatureCollection(sourceSchema, feats);
        writer.writeFeatures(features);
    }
}

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.