Tutorials Blackberry How to save data using PersistentStore in blackberry

How to save data using PersistentStore in blackberry

This sample explains “How to save data using PersistentStore in blackberry”. Example is a login screen demo implementing single signon method. It saves the username and password once we can log in successfully and retrieve from the database on later attempts.

Step-1:

We need to have a login screen with username and password text field with a submit button. As this is complete basics of UI stuffs. i left this up-to the users. Here I will completely deal with the Database/ Persistence store concepts.

Step-2:

A bean class whose object is used for saving the credentials.

package com.javatechig.data;
public class LoginDetails implements Persistable {
    private String username;
    private String password;

    public LoginDetails(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

Step-3:

Now, we can write the class to deal with the Persistence store. Here i am performing the simple operation with DB like adding object, retrieving object and then delete.

package com.javatechig.database;
import java.util.Vector;
import net.rim.device.api.system.CodeSigningKey;
import net.rim.device.api.system.ControlledAccess;
import net.rim.device.api.system.NonPersistableObjectException;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
import net.rim.device.api.ui.component.Dialog;
import com.flurry.blackberry.FlurryAgent;
import com.javatechig.data.LoginDetails;

public class LoginDatabase {

    public final static long key = 0xd3545eeea34045;
    static PersistentObject store;
    Vector objectsList;

    CodeSigningKey codeSigningKey = CodeSigningKey.get("ACME");

    public void reset() {
        try {
            store = PersistentStore.getPersistentObject(key);
            CodeSigningKey codeSigningKey = CodeSigningKey.get("ACME");
            synchronized (store) {
                objectsList = new Vector();
                store.setContents(new ControlledAccess(objectsList, codeSigningKey));
                store.commit();
            }
        }
        catch (Exception e) {
            Dialog.inform(e.toString());
        }
    }

    public LoginDatabase() {
        try {
            store = PersistentStore.getPersistentObject(key);
            CodeSigningKey codeSigningKey = CodeSigningKey.get("ACME");
            try {
                objectsList = (Vector) store.getContents(codeSigningKey);
                try {
                    if (objectsList.equals(null)) {
                        objectsList = new Vector();
                    }
                }
                catch (Exception e) {
                    objectsList = new Vector();
                }
            }
            catch (Exception ne) {
                Dialog.alert("Persistant Store : " + ne.toString());
            }
            synchronized (store) {
                store.setContents(new ControlledAccess(objectsList, codeSigningKey));
                store.commit();
            }
        }
        catch (NonPersistableObjectException e) {
            System.out.println(e.getObject());
            Dialog.alert("Persistant Store : " + e.getObject());
        }
        catch (Exception e) {
            Dialog.inform(e.toString());
        }
    }

    public void addObject(LoginDetails object) {
        try {
            synchronized (store) {
                objectsList = (Vector) store.getContents(codeSigningKey);
            }
            objectsList.addElement(object);
            synchronized (store) {
                store.setContents(new ControlledAccess(objectsList, codeSigningKey));
                store.commit();
            }
        }
        catch (Exception e) {
            Dialog.inform(e.toString());
        }
    }

    public void setCredentials(LoginDetails credentials) {
        try {
            synchronized (store) {
                objectsList = (Vector) store.getContents(codeSigningKey);
            }
            objectsList.removeAllElements();
            objectsList.addElement(credentials);
            synchronized (store) {
                store.setContents(new ControlledAccess(objectsList, codeSigningKey));
                store.commit();
            }
        }
        catch (Exception e) {
            Dialog.inform(e.toString());
        }
    }

    public Vector getObjects() {
        synchronized (store) {
            Vector objectsList = (Vector) store.getContents(codeSigningKey);
            return objectsList;
        }
    }

    public void deleteObject(LoginDetails savedObject) {
        try {
            synchronized (store) {
                Vector objectsList = (Vector) store.getContents(codeSigningKey);
                objectsList.removeElement(savedObject);
            }
            synchronized (store) {
                store.setContents(new ControlledAccess(objectsList, codeSigningKey));
                store.commit();
            }
        }
        catch (Exception e) {           
            Dialog.inform(e.toString());
        }
    }
}

Step-4:

Now we can use this below method to retrieve the data from db and then display it to the UI.

public static LoginDetails getStoredCredentials() {
    LoginDatabase ld = new LoginDatabase();
    Vector v = ld.getObjects();
    LoginDetails loginDetails = null;
    int size = v.size();
    if (size return loginDetails;
}
else {
    loginDetails = (LoginDetails) v.elementAt(0);
    return loginDetails;
}
}

This method returns the LoginDetails object. We can write this method in our UI class and then retrieving the username using getUsername() method and password using getPassword().Here we go!

Total
0
Shares
Previous Post
Blackberry Tutorials

Standard Title Bar in Blackberry

Next Post
Softwares and tools used for development

Getting Started with Airplay SDK for Mobile Application Development

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All