Saya sebenarnya pengin ngomong banyak untuk post ini, tapi kayaknya saya mending ikutan kata-kata "Talk less do more". Intinya pada post ini saya ingin memberi cobtoh bagaimana menyimpan data dengan menggunakan RecordStore pada J2ME.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Engine;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.List;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;
/**
*
* @author Hendrawan
*/
public class Friends {
private Database.Friends FriendsDataNode = new Database.Friends();
private boolean ExistingDB = false;
private RecordStore DataRecordStore = null;
private int[] FriendsDataID;
private Exception except = new Exception();
private Image image;
// static data
private String DbName = "Friends";
public Exception getExcept() {
return except;
}
public javax.microedition.lcdui.Image loadImage(String location) {
try {
return javax.microedition.lcdui.Image.createImage(location);
} catch (java.io.IOException e) {
throw new RuntimeException("Unable to load Image: " + e);
}
}
public Image getImage() {
if (image == null) {
// write pre-init user code here
try {
image = Image.createImage("/none1.jpg");
} catch (java.io.IOException e) {
e.printStackTrace();
}
// write post-init user code here
}
return image;
}
private boolean ExistingData(String recordStoreName) {
boolean existingOrNot = false;
if (recordStoreName.length() > 32)
return false;
try {
DataRecordStore = RecordStore.openRecordStore(recordStoreName, false);
existingOrNot = true;
} catch (RecordStoreNotFoundException e) {
existingOrNot = false;
} catch (Exception e) {
except = e;
} finally {
try {
DataRecordStore.closeRecordStore();
} catch (Exception e) {
except = e;
}
}
return existingOrNot;
}
private void OpenDataBase() {
ExistingDB = ExistingData(DbName);
if (ExistingDB) {
try {
DataRecordStore = RecordStore.openRecordStore(DbName, false);
} catch (Exception e) {
except = e;
}
} else {
try {
DataRecordStore = RecordStore.openRecordStore(DbName, true);
} catch (Exception e) {
except = e;
}
}
}
public Database.Friends getFriendsDataNode() {
return FriendsDataNode;
}
public boolean SetFriendsData(String aProfileID, String aName, String aPhoneNumber, String aImage) {
try {
FriendsDataNode.setProfileID(aProfileID);
FriendsDataNode.setName(aName);
FriendsDataNode.setPhoneNumber(aPhoneNumber);
FriendsDataNode.setImage(aImage);
} catch (Exception e) {
except = e;
return false;
}
return true;
}
public int CountData() throws RecordStoreNotOpenException {
OpenDataBase();
return DataRecordStore.getNumRecords();
}
public boolean GetFriendsData(List aListData) throws RecordStoreNotOpenException {
//clear all content of list
for(int i = (aListData.size()-1); i >= 0; i--) {
aListData.delete(i);
}
OpenDataBase();
int listCounter = 0;
int recordID;
try {
//record enumeration to get record data
RecordEnumeration re = DataRecordStore.enumerateRecords(null, null, false);
//create integer array to store recordID by ListIndex
FriendsDataID = new int[DataRecordStore.getNumRecords()];
// get record data
// Get RecordID
while(re.hasNextElement()) {
recordID = re.nextRecordId();
FriendsDataNode.changeFromByteArray(DataRecordStore.getRecord(recordID));
aListData.insert(listCounter, FriendsDataNode.getName() + "\n" + FriendsDataNode.getPhoneNumber(), loadImage("/" + FriendsDataNode.getImage()));
FriendsDataID[listCounter] = recordID;
listCounter++;
}
} catch (Exception e) {
except = e;
return false;
} finally {
try {
DataRecordStore.closeRecordStore();
} catch (Exception e) {
except = e;
}
}
return true;
}
public boolean GetFriendData(int aSeletedListIndex) throws RecordStoreNotOpenException, RecordStoreException {
// Cek jika DB sudah dibuat atau belum
OpenDataBase();
try {
FriendsDataNode.changeFromByteArray(DataRecordStore.getRecord(FriendsDataID[aSeletedListIndex]));
} catch (Exception e) {
except = e;
return false;
} finally {
DataRecordStore.closeRecordStore();
}
return true;
}
public boolean SaveFriendsData() throws RecordStoreNotOpenException, RecordStoreException {
// Cek jika DB sudah dibuat atau belum
OpenDataBase();
try {
byte[] FriendsData = FriendsDataNode.changeToByteArray();
if(FriendsData.length == 0) {
return false;
} else {
DataRecordStore.addRecord(FriendsData, 0, FriendsData.length);
}
} catch (Exception e) {
except = e;
return false;
} finally {
try {
DataRecordStore.closeRecordStore();
} catch (Exception e) {
except = e;
}
}
return true;
}
public boolean DeleteFriendData(int aSeletedListIndex) {
// Cek jika DB sudah dibuat atau belum
OpenDataBase();
try {
DataRecordStore.deleteRecord(FriendsDataID[aSeletedListIndex]);
} catch (Exception e) {
except = e;
} finally {
try {
DataRecordStore.closeRecordStore();
} catch (Exception e) {
except = e;
}
}
return true;
}
}
-------------awal code-----------------------
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Database;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
/**
*
* @author Hendrawan
* @blog http://blogehendrawan.blogspot.com
* @version 1.0
*/
public class Friends {
private String ProfileID;
private String Name;
private String PhoneNumber;
private String Image;
private Exception except;
public Exception getExcept() {
return except;
}
public String getProfileID() {
return ProfileID;
}
public void setProfileID(String ProfileID) {
this.ProfileID = ProfileID;
}
public String getName() {
return Name;
}
public void setName(String Name) {
this.Name = Name;
}
public String getPhoneNumber() {
return PhoneNumber;
}
public void setPhoneNumber(String PhoneNumber) {
this.PhoneNumber = PhoneNumber;
}
public String getImage() {
return Image;
}
public void setImage(String Image) {
this.Image = Image;
}
public byte[] changeToByteArray() {
byte[] data = null;
try {
ByteArrayOutputStream dataFriendsByteArrayOutStream = new ByteArrayOutputStream();
DataOutputStream dataFriendsOutputStream = new DataOutputStream(dataFriendsByteArrayOutStream);
dataFriendsOutputStream.writeUTF(ProfileID);
dataFriendsOutputStream.writeUTF(Name);
dataFriendsOutputStream.writeUTF(PhoneNumber);
dataFriendsOutputStream.writeUTF(Image);
data = dataFriendsByteArrayOutStream.toByteArray();
dataFriendsByteArrayOutStream.close();
dataFriendsOutputStream.close();
} catch (Exception e) {
except = e;
}
return data;
}
public void changeFromByteArray(byte[] data) {
try {
ByteArrayInputStream dataFriendsByteArrayInputStream = new ByteArrayInputStream(data);
DataInputStream dataFriendsDataInputStream = new DataInputStream(dataFriendsByteArrayInputStream);
ProfileID = dataFriendsDataInputStream.readUTF();
Name = dataFriendsDataInputStream.readUTF();
PhoneNumber = dataFriendsDataInputStream.readUTF();
Image = dataFriendsDataInputStream.readUTF();
dataFriendsByteArrayInputStream.close();
dataFriendsDataInputStream.close();
} catch (Exception e) {
except = e;
}
}
}
-------------akhir code-----------------------
Feb 7, 2009
Powered by Blogger.
apa datanya bisa persistence? belum saya coba sih.
thanks for sharing
kawan bagaimana caranya biar bisa diinstall aplikasi J2ME kta dalam handphone..soalnya gagal mulu..baru sony doank yang sukses..tolomh jawab
nich contohnya..http://www.4shared.com/file/JSez2wEK/uji_coba_aplikasi.html
via email muhienj90@gmail.com
Post a Comment