[Solved] Error while importing shapefile into Oracle db using geotools

gizmogaurav Asks: Error while importing shapefile into Oracle db using geotools
I am trying to import shape file into Oracle database but I am running TNS listener error:

ORA-12505, TNS:listener does not currently know of SID given in connect descriptor The Connection descriptor used by the client was:

I have the service name of the database not the SID which I am passing in the params.put(JDBCDataStoreFactory.DATABASE.key, "test")

Does this can only take SID/Service Name because its internally making the URL like

jdbc:oracle:thin:@test:1521:test. I guess it should be jdbc:oracle:thin:@test:1521/test instead of :

I was able to make a connection via SQL Developer. I’m not sure what could be incorrect here.

Below is the Code for the same

Code:
package com.polk.geotools.gis.QuickStart;

import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import org.geotools.data.DataStore;
import org.geotools.data.DataUtilities;
import org.geotools.data.DefaultTransaction;
import org.geotools.data.FeatureStore;
import org.geotools.data.Transaction;
import org.geotools.data.oracle.OracleNGDataStoreFactory;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.feature.FeatureIterator;
import org.geotools.feature.simple.SimpleFeatureBuilder;
import org.geotools.jdbc.JDBCDataStoreFactory;
import org.opengis.feature.simple.SimpleFeature;
import org.opengis.feature.simple.SimpleFeatureType;
import org.opengis.feature.type.AttributeDescriptor;
import org.opengis.filter.Filter;
import org.opengis.referencing.FactoryException;

public class ShapeToSdo {
    public  ShapeToSdo (String shploc) throws FactoryException, MalformedURLException, IOException {

        String path = shploc;
                ShapefileDataStore shp = new ShapefileDataStore(new File(path).toURL());
                //THIS IS CRUCIAL FOR GREEK CHARS parsing
                //shp.setCharset(Charset.forName("ISO-8859-7"));
                //IF NOT GREEK JUST OMMIT IT OR SET IT TO UTF_8
                shp.setCharset(Charset.forName("UTF-8"));
                Map<String, Object> params = new HashMap<String, Object>();
                params.put(JDBCDataStoreFactory.USER.key, "test");
                params.put(JDBCDataStoreFactory.PASSWD.key, "test");
                params.put(JDBCDataStoreFactory.HOST.key, "test");
                params.put(JDBCDataStoreFactory.PORT.key, "1521");
                params.put(JDBCDataStoreFactory.DATABASE.key, "test");
                params.put(JDBCDataStoreFactory.DBTYPE.key, "oracle");
                DataStore oracle = new OracleNGDataStoreFactory().createDataStore(params);
                if(oracle != null && oracle.getTypeNames() != null)
                    System.out.println("Oracle connected");
                System.out.println("oracle.getTypeNames()"+oracle.getTypeNames());
                String typeName = shp.getTypeNames()[0].toUpperCase();

                System.out.println("shp.getTypeNames()[0]===="+shp.getTypeNames()[0]);
                if(!Arrays.asList(oracle.getTypeNames()).contains(typeName)){
                    System.out.println("shp.getSchema()"+shp.getSchema());
                    oracle.createSchema(shp.getSchema());
                }
                FeatureStore oraStore = (FeatureStore) oracle.getFeatureSource(typeName);
                oraStore.removeFeatures(Filter.INCLUDE);
                
                SimpleFeatureType targetSchema = (SimpleFeatureType) oraStore.getSchema();
                SimpleFeatureBuilder builder = new SimpleFeatureBuilder(targetSchema);
                
                
                FeatureIterator fi = shp.getFeatureSource().getFeatures().features();
                SimpleFeatureType sourceSchema = shp.getSchema();
                
                Transaction t = new DefaultTransaction();
                oraStore.setTransaction(t);
                while(fi.hasNext()) {
                    SimpleFeature source = (SimpleFeature) fi.next();
                
                    for(AttributeDescriptor ad : sourceSchema.getAttributeDescriptors()) {
                        String attribute = ad.getLocalName();
                        String passAtrr = attribute.toUpperCase();
                        
                        builder.set(passAtrr, source.getAttribute(attribute));
                        }
                    
                    oraStore.addFeatures(DataUtilities.collection(builder.buildFeature(null)));
                }
                t.commit();
                t.close();
                
            }
    public static void main(String[] args) {
        try {
            ShapeToSdo so = new ShapeToSdo("D:\Git\GIS spatial changes\Shoreline Files\county_spatial_erase_poc.shp");
        } catch (FactoryException | IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
        }

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.