Hi all,

Using mainly the examples I've managed to get shapefile reading working and
am looping through each feature one at a time.  I now want to write these to
Oracle and have created a connection to the db which is at least connecting
fine but have a few questions...


   - To make an exact copy of the table in another datastore presumably you
   would first get the schema of the source using getSchema() and then create
   that schema in the destination using createSchema()?
   - In the class OracleDataStore it says this method is "not currently
   supported".  If it were supported should it take care of actually creating
   the database table with the appropriate field types?  If so I assume
   therefore there is no way in geotools at present to create a db table in
   Oracle?
   - After that I'm a bit confused as to what to do next.  Assuming I do
   have a table already the OracleDataStore class has a function called
   createFeatureWriter.  This says it creates a text based feature writer which
   just issues SQL direct to the db but how do I get the features I've read
   from the shapefile into it?  It has args for a reader, so do I not have to
   iterate through the reader, extract the geometry and then pass it to the
   writer?  Help!

My code so far is shown below, thanks in advance,

Tom



Here's the code, note, lines beginning with * are failing compile.  + I'm
not a java developer by trade!

public class Main {
    public static void main(String[] args)
    {
        try
        {
            File file = new File( "c:/myfile.shp" );
            if( !file.exists() ) System.exit(1);

            Map connect = new HashMap();
            connect.put( "url", file.toURI().toURL() );

            DataStore dataStore = DataStoreFinder.getDataStore( connect );
            String[] typeNames = dataStore.getTypeNames ();
            String typeName = typeNames[0];

            FeatureSource featureSource = dataStore.getFeatureSource(
typeName );

            // get the field names and types
            AttributeType[] at =
featureSource.getSchema().getAttributeTypes();

            Iterator j = Arrays.asList(at).iterator();
            while (j.hasNext())
            {
                System.out.println(j.next());
            }

            // now create and open a connection to Oracle
            Map oraconnect = new HashMap();
            oraconnect.put("dbtype", "oracle");
            oraconnect.put("host", "myhost");
            oraconnect.put("port", "1521");
            oraconnect.put("instance", "myinstance");
            oraconnect.put("user", "myuser");
            oraconnect.put("passwd", "mypassword");

            DataStore orads = new
OracleDataStoreFactory().createDataStore(oraconnect);

            DefaultTransaction transaction = new DefaultTransaction("test");
            *JDBCFeatureWriter aWriter =
orads.createFeatureWriter(transaction);


            FeatureCollection collection = featureSource.getFeatures();
            FeatureIterator fc = collection.features();

            Feature feature;
            try
            {
                while(fc.hasNext())
                {
                    feature = (Feature) fc.next();

                    Feature aNewFeature = (Feature) aWriter.next();
                    *aNewFeature.setAttributes(feature.getAttributes());
                    aWriter.write();
                    transaction.commit();
                }
            }
            catch (Exception e)
            {
                transaction.rollback();
                System.out.println(e.getMessage());
            }
            finally
            {
                //fc.close(iterator);
                transaction.close();
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Geotools-gt2-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/geotools-gt2-users

Reply via email to