wiki:MolgenisDatabase

Accessing the database via Molgenis Database

MOLGENIS Database class provides efficient methods to add, update, remove, find and count data inside your MOLGENIS either one-by-one or in an efficient batch modus.

In a standalone program

Assuming you use the MolgenisAddressBookExample you can create and execute the following file:

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import addressbook.data.JDBCDatabase;
import addressbook.data.types.Contact;

public class TestDatabaseApi 
{

  public static void main(String[] args) throws Exception 
  {
    //Create a database from the properties file
    Database db = new addressbook.data.JDBCDatabase("molgenis.properties");

    //List all contacts:
    for(Contact c: db.find(Contact.class))
    {
       System.out.println(c);
    }
                
    //Add one object to the database:
    Contact one = new Contact();
    one.setDisplayname("testone");
    db.add(one);
                
    //Add many objects efficiently in batch
    System.out.println("add many contacts:");
    List<Contact> many = new ArrayList<Contact>();
    for(int i = 0; i < 10; i++)
    {
      Contact c = new Contact();
      c.setDisplayname("simulation"+i);
      many.add(c);
    }
    db.add(many);               

    //Observe that after insert the autoid 'id' colunmn is set
    for(Contact c: many)
    {
      System.out.println(c);
    }
                
    //Finally, remove all the data you just added:
    System.out.println("remove contacts:");
    db.remove(one);
    db.remove(many);    
  }
}