This example shows how to add new feature data into a MySQL database.
First you have to create the mockup database and then the roads table with the following script
# Host: localhost # Database: mokup # Table: 'roads' # CREATE TABLE `roads` ( `width` int(8) default NULL, `the_geom` linestring default NULL ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Then you can access and update the database by the following code
//feature : type creation AttributeType roadWidth = AttributeTypeFactory.newAttributeType( "width", Integer.class); AttributeType geom = AttributeTypeFactory.newAttributeType("the_geom", LineString.class); FeatureType ftRoad = FeatureTypeFactory.newFeatureType( new AttributeType[] { roadWidth, geom }, "roads"); // feature : instance creation WKTReader wktReader = new WKTReader(); LineString geometry = (LineString) wktReader .read("LINESTRING (0 0, 10 10)"); Float width = new Float(10); Feature theRoad = ftRoad.create(new Object[] { width,geometry }, "myRoad"); // the order must be compliant with the one in MySQL // datastore creation MySQLDataStoreFactory factory = new MySQLDataStoreFactory(); Map params = new HashMap(); params.put( "database", "mockup" ); params.put( "dbtype", "mysql"); params.put( "host", "localhost"); params.put( "port", "3307"); params.put( "user", "mockuplog"); params.put( "passwd", "mockuppass"); MySQLDataStore datastore = (MySQLDataStore) factory.createDataStore( params ); LOGGER.info("created new datastore"); //road table creation //datastore.createSchema(ftRoad); // Not yet implemented!!! but should be the way to do it.So create the table before FeatureStore fsRoads = (FeatureStore)(datastore.getFeatureSource("roads")); FeatureReader aReader = DataUtilities.reader(new Feature[] { theRoad }); fsRoads.addFeatures(aReader); //it is all folk!!!


