Pada aplikasi J2ME juga mengenal database
Penyimpanan informasi/data pada perangkat mobile di J2ME dapat menggunakan dua cara yaitu:
a. Menyimpan data pada suatu file yang diletakkan pada direktori res
b. Menggunakan Record Management System (RMS)
Membuat sebuah file untuk penyimpanan database. Kelas RecordStore pada RMS berfungsi menyediakan fitur untuk menyimpan, mengupdate, mengambil dan menghapus data pada perangkat mobile. Kelas RecordStore merepresentasikan sebuah lokasi penyimpanan data permanen. Satu RecordStore dapat digunakan oleh lebih dari satu MIDlet baik pada saat bersamaan maupun terpisah. Berdasarkan spesifikasi MIDP, vendor perangkat mobile bertanggung jawab pada pemeliharaan integritas obyek RecordStore.
Untuk contoh bisa dicoding/ juga bisa copy-paste. Monggo!!!
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Displayable;
import javax.microedition.midlet.MIDlet;
import java.io.IOException;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import javax.microedition.lcdui.ChoiceGroup;
public class PolingWebMidlet extends MIDlet implements CommandListener {
private Form form;
private TextField tfName1, tfName2;
private ChoiceGroup cgPoling;
private Command cmdRetrieve, cmdSave, cmdExit;
private UserOptionRS userStore;
public PolingWebMidlet() {
userStore = new UserOptionRS();
//tfName1 = new TextField("First Name :", "", 15, TextField.ANY);
//tfName2 = new TextField("Last Name :", "", 15, TextField.ANY);
cgPoling = new ChoiceGroup("Apakah Hoby anda : ", ChoiceGroup.EXCLUSIVE);
cgPoling.append("Musik", null);
cgPoling.append("Programmer", null);
cgPoling.append("Teknisi", null);
cgPoling.append("Marketing", null);
cgPoling.setSelectedIndex(0, true);
cmdSave = new Command("Pilih", Command.SCREEN, 1);
cmdRetrieve = new Command("Lihat", Command.SCREEN, 2);
cmdExit = new Command("Exit", Command.EXIT, 1);
Form form = new Form("Hoby terbanyak : ");
//form.append(tfName1);
//form.append(tfName2);
form.append(cgPoling);
form.addCommand(cmdSave);
form.addCommand(cmdRetrieve);
form.addCommand(cmdExit);
form.setCommandListener(this);
Display.getDisplay(this).setCurrent(form);
}
protected void startApp() {}
protected void pauseApp() {}
protected void destroyApp(boolean force) {}
public void commandAction(Command c, Displayable d) {
if (c == cmdSave) {
saveOption();
//tfName1.setString("");
//tfName2.setString("");
cgPoling.setSelectedIndex(0, true);
} else if (c == cmdRetrieve) {
Alert a = new Alert("Hasil Poling: ");
a.setTimeout(Alert.FOREVER);
a.setString(retrieveOption());
Display.getDisplay(this).setCurrent(a);
} else if (c == cmdExit) {
userStore.close();
notifyDestroyed();
}
}
private String retrieveOption() {
String list="", data;
int musik=0, programmer=0, teknisi=0, marketing=0;
try{
for(int i=1; i<=userStore.countRecords(); i++){
ByteArrayInputStream bOptions = new ByteArrayInputStream(userStore.readOption(i));
DataInputStream dOptions = new DataInputStream(bOptions);
//list = list + dOptions.readUTF() + " " + dOptions.readUTF() + "\n";
//list = list + dOptions.readUTF() + "\n";
data = dOptions.readUTF();
//if(dOptions.readUTF() != null){
if(data.equals("Musik")){
musik = musik + 1;
}else if(data.equals("Programmer")){
programmer = programmer + 1;
}else if(data.equals("Teknisi")){
teknisi = teknisi + 1;
}else if(data.equals("Marketing")){
marketing = marketing +1;
}else{
}
//}
dOptions.close();
bOptions.close();
}
list = "Musik: "+ musik +"\nProgrammer: "+ programmer +"\nTeknisi: "+ teknisi +"\nMarketing: "+ marketing +"\n";
} catch(IOException e) { e.printStackTrace(); }
return list;
}
private void saveOption() {
try {
ByteArrayOutputStream bOptions = new ByteArrayOutputStream();
DataOutputStream dOptions = new DataOutputStream(bOptions);
//dOptions.writeUTF(tfName1.getString());
//dOptions.writeUTF(tfName2.getString());
int a = cgPoling.getSelectedIndex();
dOptions.writeUTF(cgPoling.getString(a));
userStore.writeOption(bOptions.toByteArray());
dOptions.close();
bOptions.close();
} catch(IOException e) { e.printStackTrace(); }
}
}
Dan harus membuat userOptionRS.java untuk databasenya.
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
public class UserOptionRS {
private String storeName = "Poling";
private RecordStore rsUserOption;
private byte[] currentUserOption;
public UserOptionRS() {
try {
rsUserOption = RecordStore.openRecordStore(storeName, true);
} catch(RecordStoreException e) {
e.printStackTrace();
}
}
int countRecords(){
int numRecords;
numRecords = 0;
try {
numRecords = rsUserOption.getNumRecords();
} catch(RecordStoreException e) {
e.printStackTrace();
}
return numRecords;
}
public byte[] readOption(int rowID) {
try {
currentUserOption = rsUserOption.getRecord(rowID);
} catch(RecordStoreException e) {
e.printStackTrace();
}
return currentUserOption;
}
public void writeOption(byte[] option) {
try {
rsUserOption.addRecord(option, 0, option.length);
} catch(RecordStoreException e) {
e.printStackTrace();
}
currentUserOption = option;
}
public void close() {
try {
rsUserOption.closeRecordStore();
//rsUserOption.deleteRecordStore(storeName);
} catch(RecordStoreException e) {
e.printStackTrace();
}
}
}
Komentar