Android

Here is a simple example on how to use NeoDatis ODB on Google Android platform:

  • Just download the NeoDatis release (there is no specific version for Android)
  • We assume you are using the Eclipse plugin for Android
  • Put the jar in the assets directory (this directory was created by the eclipse plugin). This will enable Android to use NeoDatis lib on the cell phone.
  • Adds this jar to your project build path
  • file path : the only trick here is the place where the NeoDatis file will be stored on the Android (linux) file system. Make sure to execute these lines to build the correct absolute file name:
File directory = getDir("data", Context.MODE_PRIVATE);
String fileName = directory.getAbsolutePath()+"/test-android.neodatis";

Here is our example project:

package android.hello;
 
import java.io.File;
import java.util.Date;
 
import org.neodatis.odb.NeoDatis;
import org.neodatis.odb.Objects;
import org.neodatis.tool.StringUtils;
 
import android.app.Activity;
import android.content.Context;
import android.hello.vo.User;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
 
public class HelloActivity extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
 
        Log.i("neodatis","Starting Android app with NeoDatis odb");
 
        final TextView tv = new TextView(this);
        tv.setText("Waiting");
        try{
            // Execute NeoDatis and return a summary of the execution
            String s = testdb();
            tv.setText(s);
        }catch (Exception e) {
            tv.setText(StringUtils.exceptionToString(e, true));
        }
 
        setContentView(tv);
    }
 
    /**
     * This method stores a new user in the database and then retrieve all users
     * @return A String with the number of users in the database
     */
    private String testdb(){
        ODB odb = null;
 
        StringBuffer buffer = new StringBuffer();
 
        try{
            // Ask Android where we can store our file
            File directory = getDir("data", Context.MODE_PRIVATE);
            String fileName = directory.getAbsolutePath()+"/test-android.neodatis";
 
            // Opens the NeoDatis database
            odb = NeoDatis.open(fileName);
 
            // Stores a new user
            OID oid = odb.store(new User("Olivier Smadja", new Date()));
 
            // Close the database
            odb.close();
 
            // Checks if NeoDatis file exists
            buffer.append("File exists = " + new File(fileName).exists());
 
            // Re-open the database
            odb = NeoDatis.open(fileName);
 
            // Retrieve all users 
            Objects<User> users = odb.query(User.class).objects();
 
            // Add the number of users to our string buffer
            buffer.append("\nNb Objects=").append(users.size());
 
            return buffer.toString();
        }catch (Throwable e) {
            if(odb!=null){
                odb.rollback();
            }
            return StringUtils.exceptionToString(e, true);
        }finally{
            // Close the NeoDatis database
            if(odb!=null){
                odb.close();
            }
        }
 
    }
}

The User class definition:

package android.hello.vo;
 
import java.util.Date;
 
public class User {
    private String name;
    private Date birthdate;
    public User() {
        super();
    }
    public User(String name, Date birthdate) {
        super();
        this.name = name;
        this.birthdate = birthdate;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Date getBirthdate() {
        return birthdate;
    }
    public void setBirthdate(Date birthdate) {
        this.birthdate = birthdate;
    }
 
    @Override
    public String toString() {
        return name + ", "+birthdate;
    }
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License