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
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();
}
}
}