Here in this post we will see how to store customer data using PersistentStore.
In my DataObject class used to store customer data. it has 2 parameters customer ID and some string property it may be name address or any other parameter. Also you can change those parameters according to your requirements. we are suing vector to store those pairs in runtime. Hope this will useful for you
In my DataObject class used to store customer data. it has 2 parameters customer ID and some string property it may be name address or any other parameter. Also you can change those parameters according to your requirements. we are suing vector to store those pairs in runtime. Hope this will useful for you
============================================
package com.DataStore;
import java.util.Vector;
import net.rim.device.api.util.Persistable;
public final class DataObject implements Persistable
{
private Vector elements;
public static final int CLIENT_ID = 0;
public DataObject()
{
elements = new Vector(1);
int capacity = elements.capacity();
for (int i = 0; i < capacity; ++i)
{
elements.addElement(new String(""));
}
}
/**
* @param id
* @return String
* String getElement(int id)
*/
public String getElement(int id)
{
return (String) elements.elementAt(id);
}
public void setElement(int id, String value)
{
elements.setElementAt(value, id);
}
}
==============================================
package com.DataStore;
import java.util.Vector;
import net.rim.device.api.system.PersistentObject;
import net.rim.device.api.system.PersistentStore;
public class DataHandler {
private static Vector data;
private static PersistentObject store;
static
{
store = PersistentStore.getPersistentObject(1);
synchronized (store) {
if (store.getContents() == null) {
store.setContents(new Vector());
store.commit();
}
}
data = (Vector) store.getContents();
}
/**
* @param info
* static void saveObject(DataObject info)
*/
public static void saveObject(DataObject info)
{
data.addElement(info);
synchronized (store) {
store.setContents(data);
store.commit();
}
}
/**
* @return String
* static String getClient()
*/
public static String getClient() {
synchronized (store) {
data = (Vector) store.getContents();
if (!data.isEmpty()) {
DataObject info = (DataObject) data.lastElement();
return info.getElement(DataObject.CLIENT_ID);
}
else
{
//If data is empty here we return empty string
return "";
}
}
}
}










