[Gvsig_english] How to open, modify and edit a dbasefile ?

Matthieu Voorons matthieu.voorons at gmail.com
Thu May 17 17:27:40 CEST 2007


Hi Vicente,
Thanks for your quick reply.
I've wrote a test to read a dbf file based on your suggestions :

        String filename = "C:\\properties.dbf";
        LayerFactory.getDataSourceFactory().addFileDataSource("gdbms dbf 
driver", "properties.dbf", filename);
        DataSource dataSource;
        try {
            dataSource = 
LayerFactory.getDataSourceFactory().createRandomDataSource("properties.dbf",  
DataSourceFactory.AUTOMATIC_OPENING);
            SelectableDataSource sds;
            sds = new SelectableDataSource(dataSource);
            EditableAdapter auxea = new EditableAdapter();
            auxea.setOriginalDataSource(sds);
        } catch (DriverException ex) {
            ex.printStackTrace();
        } catch (DriverLoadException ex) {
            ex.printStackTrace();
        } catch (NoSuchTableException ex) {
            ex.printStackTrace();
        }

but it results in :

Exception in thread "main" java.lang.NullPointerException
        at 
com.hardcode.gdbms.engine.data.DataSourceFactory.getDriver(Unknown Source)
        at 
com.hardcode.gdbms.engine.data.DataSourceFactory.createRandomDataSource(Unknown 
Source)
        at 
com.hardcode.gdbms.engine.data.DataSourceFactory.createRandomDataSource(Unknown 
Source)
        at testdbf.Main.main(Main.java:52)
Java Result: 1

Apparently the dbf driver is not loaded, do I have to set a path to some 
libs or register a driver ?
Thanks for you time,
Regards
Matthieu


Vicente Caballero Navarro a écrit :
> Hello.
> You are trying it at too low level.  You should better try it in an 
> upper-level way
>
> To create an access to a dbf you may have a look to the piece of code 
> in the method createFromGUI in the class ProjectTableFactory at appgvSIG
>
> LayerFactory.getDataSourceFactory().addFileDataSource("gdbms dbf driver",
>                   name, "C:\\properties.dbf");
> DataSource dataSource = LayerFactory.getDataSourceFactory()
>                   .createRandomDataSource(name,
>                   DataSourceFactory.AUTOMATIC_OPENING);
> SelectableDataSource sds = new SelectableDataSource(dataSource);
> EditableAdapter auxea = new EditableAdapter();
> auxea.setOriginalDataSource(sds);
>
>
> To make sql queries, in the extension called FiltroExtension we have 
> methods to which the expression is passed to as a parameter and return 
> a vector of long with the Selection produced by such query.
>
> private long[] doSet(String expression) {
>        try {
>            DataSource ds = 
> LayerFactory.getDataSourceFactory().executeSQL(expression,
>                    DataSourceFactory.MANUAL_OPENING);
>
>            return ds.getWhereFilter();
>        } catch (DriverLoadException e) {
>            
> JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage()); 
>
>        } catch (com.hardcode.gdbms.engine.data.driver.DriverException 
> e) {
>            
> JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"driver_error")+"\n"+e.getMessage()); 
>
>        } catch (ParseException e) {
>            
> JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage()); 
>
>        } catch (SemanticException e) {
>            
> JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"semantic_expresion_error")+"\n"+e.getMessage()); 
>
>        } catch (IOException e) {
>            
> JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"input_output_error")+"\n"+e.getMessage()); 
>
>        } catch (EvaluationException e) {
>            
> JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"parse_expresion_error")+"\n"+e.getMessage()); 
>
>        } catch (com.hardcode.gdbms.parser.TokenMgrError e) {
>            
> JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(),PluginServices.getText(this,"expresion_error")+"\n"+e.getMessage()); 
>
>        }
>
>        return null;
>    }
>
>
>
>
>
>
> Matthieu Voorons escribió:
>> Hi list,
>> I'm trying to develop a new extension for gvsig. I wish to open the 
>> dbf file related to a shapefile, modify it (add some fields and 
>> change some entries) and make some sql queries. How could I do that ?
>> I've seen that there is a dbf driver in the package 
>> com.iver.cit.gvsig.fmap.drivers.dbf, and a dbf reader in the package 
>> com.iver.cit.gvsig.fmap.drivers.shp. Which one should I use ?
>> I've played a little with the code, the DbaseFileNIO and 
>> DbaseFileHeaderNIO.
>> Reading the header is not a problem, but I'm not able to acces the 
>> data when using DbaseFileReaderNIO :
>>
>>        String filename = "C:\\properties.dbf";
>>        File f = new File(filename);
>>        try {
>>            FileChannel in = new FileInputStream(filename).getChannel();
>>            DbaseFileReaderNIO r = new DbaseFileReaderNIO(in);
>>            int fields = r.getHeader().getNumFields();
>>            int recordCount = r.getHeader().getNumRecords();
>>            System.out.println("fields: " + fields + " recordCount: " 
>> + recordCount);// This return 0 and 0 even if there are 
>> 10                 
>>                                                                        
>>                                  // fields and 20 records
>>            String fieldUniqueValueArr[] = new String[recordCount];
>>            int j = 0;
>>            while (r.hasNext()) {
>>                DbaseFileReaderNIO.Row row = r.readRow();
>>                System.out.println(row.read(0).toString());
>>                fieldUniqueValueArr[j] = row.read(0).toString();
>>                j++;
>>           }
>>        } catch (IOException ex) {
>>            ex.printStackTrace();
>>        }
>>
>> I'm also wondering how to make a SQL query on the dbf data.
>> I would be very grateful if you could send me some code snippets or 
>> some hints to where to digg.
>>
>> Best regards,
>> Matthieu Voorons
>> _______________________________________________
>> Gvsig_internacional mailing list
>> Gvsig_internacional at runas.cap.gva.es
>> http://runas.cap.gva.es/mailman/listinfo/gvsig_internacional
>>
>>
>>
> Un saludo.
>


More information about the Gvsig_internacional mailing list