30
LAPORAN PRAKTIKUM 7 MOBILE PROGRAMMING Oleh : Dani Firmansyah (1083044) Kelas : TI / 3B

Laporan Praktikum 7 Persistence

Embed Size (px)

Citation preview

Page 1: Laporan Praktikum 7 Persistence

LAPORAN PRAKTIKUM 7

MOBILE PROGRAMMING

Oleh :

Dani Firmansyah (1083044)

Kelas : TI / 3B

JURUSAN TEKNIK INFORMATIKA

POLITEKNIK POS INDONESIA

BANDUNG

2010

Page 2: Laporan Praktikum 7 Persistence

HALAMAN PENGESAHAN

Judul Praktikum : Persistence

Sub Judul Praktikum : 1. Menambah Item

2. Membaca Record Store

3. Penggunaan Enumerator

4. Penggunaan Record Comparator

5. Penggunaan Record Filter

Tanggal Praktikum : 18 Desember 2010

Tanggal Penyerahan Laporan : 25 November 2010

Tempat Praktikum : Laboratorium Komputer 306

Alat dan Software : Sun Java Wireless Toolkit 2.5

Kelas : TI / 3B

Nama : Dani Firmansyah (1083044)

Jurusan : Teknik Informatika

Bandung, Desember 2010Menyetujui

Dosen Pengajar

Azizah Zakiah, S.Kom

Page 3: Laporan Praktikum 7 Persistence

KATA PENGANTAR

Puji syukur saya panjatkan ke hadirat Tuhan Yang Maha Esa yang atas kurniaNya kami

dapat menyelesaikan Laporan Praktikum mata kuliah Mobile Programming ini.

Adapun isi dari Laporan ini adalah mengenai Persistence

Demikian Laporan Praktikum Mobile Programming ini saya buat, sebagai hasil

praktikum yang telah saya lakukan. Kritik dan saran yang membangun sangat saya harapkan

sehingga kedepannya nanti dapat lebih baik.

Penyusun,

Page 4: Laporan Praktikum 7 Persistence

BAB I

LANDASAN TEORI

1.1 Persistence

Sebuah Record Store adalah sebuah koleksi daripada record-record. Record Id didalam Record

Store selalu unique. Record Id akan secara otomatis dialokasikan pada saat pembentukan sebuah

record dan bertindak sebagai index atau primary key. Pemberian record Id dilaksanakan secara

sekuensial dan nilai yang diberikan kepada record Id pertama pada setiap Record Store adalah 1

(satu). Pada saat sebuah record dihapus, record id-nya tidak akan bisa digunakan kembali. Jika

kita membuat empat buah record dan menghapus record ke-empat, maka record Id selanjutnya

yang akan diberikan oleh system.

MIDlets dapat menciptakan lebih dari satu Record Store. Nama dari sebuah record store didalam

MIDlet suite haruslah unique. Nama dari record store juga case sensitive dan memiliki panjang

maksimal 32 karakter. Pada saat MIDlet suite dihapus dari sebuah device, maka semua record

store yang terkoneksi dengan MIDlet didalam suite tersebut juga akan terhapus.

Page 5: Laporan Praktikum 7 Persistence

BAB II

HASIL PRAKTIKUM

PERSISTENCE

2.1 Menambah Item

import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.rms.*;public class RMSExample extends MIDlet implements CommandListener {Display display;List recList;RecordStore recStore;byte[] data = null;Command exitCommand = new Command("Exit", Command.EXIT, 1);Command newCommand = new Command("New Item", Command.OK, 1);Ticker ticker = new Ticker("Dani firmansyah - politeknik pos Indonesia");public RMSExample(){recList = new List("Record Store",List.IMPLICIT);dispRec();recList.setTicker(ticker);recList.addCommand(exitCommand);recList.addCommand(newCommand);recList.setCommandListener(this);}public void startApp() {if (display == null){display = Display.getDisplay(this);display.setCurrent(recList);}}public void pauseApp(){}public void destroyApp(boolean unconditional) {}public void commandAction(Command c, Displayable d){

Page 6: Laporan Praktikum 7 Persistence

if (c== exitCommand){destroyApp(true);notifyDestroyed();// Exit}if (c== newCommand){try{// Buka dan buatlah record store dengan nama “RmsExample1”recStore= RecordStore.openRecordStore("RmsExample1", true);// Ini adalah String yang akan kita masukkan kedalam recordString newItem="Record#" + recStore.getNextRecordID();// Konversikan String ke byte arraydata=newItem.getBytes();// Tulislah record kedalam record storerecStore.addRecord(data, 0, data.length);recStore.closeRecordStore();}catch(Exception e){System.out.println(e.toString());}dispRec();}}public void dispRec(){recList.deleteAll();String[] data = getRecordList();if(data.length>0){for(int i=0;i<data.length;i++)recList.append(data[i],null);}}public String[] getRecordList(){try{// Buka dan buatlah record store dengan nama “RmsExample1”recStore= RecordStore.openRecordStore("RmsExample1", true);// Masukkan content kedalam record storeString[] dataList= new String[recStore.getNumRecords()];if (recStore.getNumRecords()>0){for(int recId=1; recId<=recStore.getNumRecords(); recId++){int size = recStore.getRecordSize( recId );if( data == null || data.length < size ){data=new byte[size+20];}

Page 7: Laporan Praktikum 7 Persistence

//getRecordmemilikireturnvalue berupa panjang dari recordint recLength = recStore.getRecord(recId,data,0);// Mengkonversikan byte array menjadi StringdataList[recId-1] = new String(data, 0, recLength);}}recStore.closeRecordStore();return dataList;}catch (Exception e){return null;}}}

2.2 Membaca Record Store

Page 8: Laporan Praktikum 7 Persistence

import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.rms.*;public class RmsList extends MIDlet implements CommandListener {Display display;List recList;RecordStore recStore;byte[] data = null;Command exitCommand = new Command("Exit", Command.EXIT, 1);Ticker ticker = new Ticker("DANI - POLTEKPOS INDONESIA");public RmsList(){recList = new List("Record Store List",List.IMPLICIT);dispList();recList.setTicker(ticker);recList.addCommand(exitCommand);recList.setCommandListener(this);}public void startApp() {if (display == null){display = Display.getDisplay(this);display.setCurrent(recList);}}public void pauseApp(){}public void destroyApp(boolean unconditional) {}public void commandAction(Command c, Displayable d){if (c== exitCommand){destroyApp(true);notifyDestroyed();// Exit}}public void dispList(){recList.deleteAll();try{String[] data= recStore.listRecordStores();if(data.length>0){for(int i=0;i<data.length;i++)

Page 9: Laporan Praktikum 7 Persistence

recList.append(data[i],null);}}catch (Exception e){}}}

2.3 Penggunaan Enumerator

Page 10: Laporan Praktikum 7 Persistence

import javax.microedition.midlet.*;import javax.microedition.lcdui.*;import javax.microedition.rms.*;import java.io.*;public class RmsExample2 extends MIDlet implements CommandListener {Display display;List recList;RecordStore recStore;byte[] data = null;Command exitCommand = new Command("Exit", Command.EXIT, 1);Command newCommand = new Command("New Item", Command.OK, 1);Ticker ticker = new Ticker("JENI - Java Education Network Indonesia");public RmsExample2(){recList = new List("Record List",List.IMPLICIT);dispRec();recList.setTicker(ticker);recList.addCommand(exitCommand);recList.addCommand(newCommand);recList.setCommandListener(this);}public void startApp() {if (display == null){display = Display.getDisplay(this);display.setCurrent(recList);}}public void pauseApp(){}public void destroyApp(boolean unconditional) {}public void commandAction(Command c, Displayable d){if (c== exitCommand){destroyApp(true);notifyDestroyed();// Exit}if (c== newCommand){try{// Buka dan atau buatlah record store dengan nama “RmsExample2”recStore= RecordStore.openRecordStore("RmsExample2", true);ByteArrayOutputStream out = new ByteArrayOutputStream();

Page 11: Laporan Praktikum 7 Persistence

DataOutputStream dOut= new DataOutputStream(out);//MenyimpansebuahintegerdOut.writeInt(recStore.getNextRecordID()* recStore.getNextRecordID());// Menyimpan sebuah stringdOut.writeUTF("Record #" + recStore.getNextRecordID());byte[] bytes = out.toByteArray();// Menuliskan Record pada StorerecStore.addRecord(bytes,0,bytes.length);dOut.close();out.close();recStore.closeRecordStore();}catch(Exception e){System.out.println(e.toString());}dispRec();}}public void dispRec(){recList.deleteAll();try{// Membuka atau membuat sebuah record store dengan nama "RmsExample2"recStore= RecordStore.openRecordStore("RmsExample2", true);// Mengambil isi dari record storeRecordEnumeration enumerator= recStore.enumerateRecords(null, null, false);while (enumerator.hasNextElement()){//MenujuRecordselanjutnyabyte[] recBytes = enumerator.nextRecord();ByteArrayInputStream in = new ByteArrayInputStream(recBytes);DataInputStream dIn = new DataInputStream(in);intcount=dIn.readInt();String item = dIn.readUTF();recList.append(count+","+item,null);dIn.close();in.close();}recStore.closeRecordStore();}catch (Exception e){}}}

Page 12: Laporan Praktikum 7 Persistence

2.4 Penggunaan Record Comparator

import javax.microedition.midlet.*;

Page 13: Laporan Praktikum 7 Persistence

import javax.microedition.lcdui.*;

import javax.microedition.rms.*;

import java.io.*;

public class RmsComparator extends MIDlet implements

CommandListener,RecordComparator {

Display display;

List recList;

RecordStore recStore;

byte[] data = null;

Command exitCommand = new Command("Exit", Command.EXIT, 1);

Command newCommand = new Command("New Item", Command.OK, 1);

Ticker ticker = new Ticker(

"JENI - Java Education Network Indonesia");

public RmsComparator(){

recList = new List("Record List",List.IMPLICIT);

dispRec();

recList.setTicker(ticker);

recList.addCommand(exitCommand);

recList.addCommand(newCommand);

recList.setCommandListener(this);

}

public void startApp() {

if (display == null){

display = Display.getDisplay(this);

display.setCurrent(recList);

}

}

public void pauseApp(){

}

public void destroyApp(boolean unconditional) {

}

Page 14: Laporan Praktikum 7 Persistence

public void commandAction(Command c, Displayable d){

if (c== exitCommand){

destroyApp(true);

notifyDestroyed();// Exit

}

if (c== newCommand){

try{

// Buka dan atau buatlah record store dengan nama “RmsComparator”

recStore= RecordStore.openRecordStore("RmsComparator", true);

ByteArrayOutputStream out = new ByteArrayOutputStream();

DataOutputStream dOut= new DataOutputStream(out);

//Menyimpansebuahinteger

dOut.writeInt(recStore.getNextRecordID()* recStore.getNextRecordID());

// Menyimpan sebuah string

dOut.writeUTF("Record #" + recStore.getNextRecordID());

byte[] bytes = out.toByteArray();

// Menuliskan Record pada Store

recStore.addRecord(bytes,0,bytes.length);

dOut.close();

out.close();

recStore.closeRecordStore();

}catch(Exception e){

System.out.println(e.toString());

}

dispRec();

}

}

public void dispRec(){

recList.deleteAll();

try{

// Membuka atau membuat sebuah record store dengan nama "RmsComparator"

Page 15: Laporan Praktikum 7 Persistence

recStore= RecordStore.openRecordStore("RmsComparator", true);

// Mengambil isi dari record store

RecordEnumeration enumerator

= recStore.enumerateRecords(null, this, false);

while (enumerator.hasNextElement()){

//MenujuRecordselanjutnya

byte[] recBytes = enumerator.nextRecord();

ByteArrayInputStream in = new ByteArrayInputStream(recBytes);

DataInputStream dIn = new DataInputStream(in);

intcount=dIn.readInt();

String item = dIn.readUTF();

recList.append(count+","+item,null);

dIn.close();

in.close();

}

recStore.closeRecordStore();

}catch (Exception e){

}

}

public int compare(byte[] rec1, byte[] rec2){

String record1 = new String(rec1).toUpperCase();

String record2 = new String(rec2).toUpperCase();

//Sorting Ascending

if (record1.compareTo(record2) < 0){

return(PRECEDES);

} else{

if (record1.compareTo(record2) > 0){

return(FOLLOWS);

} else{

return(EQUIVALENT);

}

Page 16: Laporan Praktikum 7 Persistence

}

}

}

2.5 Penggunaan Record Filter

import javax.microedition.midlet.*;

Page 17: Laporan Praktikum 7 Persistence

import javax.microedition.lcdui.*;

import javax.microedition.rms.*;

import java.io.*;

public class RmsFilter extends MIDlet implements

CommandListener,RecordComparator,RecordFilter {

Display display;

List recList;

RecordStore recStore;

byte[] data = null;

Command exitCommand = new Command("Exit", Command.EXIT, 1);

Command newCommand = new Command("New Item", Command.OK, 1);

Ticker ticker = new Ticker(

"JENI - Java Education Network Indonesia");

public RmsFilter(){

recList = new List("Record List",List.IMPLICIT);

dispRec();

recList.setTicker(ticker);

recList.addCommand(exitCommand);

recList.addCommand(newCommand);

recList.setCommandListener(this);

}

public void startApp() {

if (display == null){

display = Display.getDisplay(this);

display.setCurrent(recList);

}

}

public void pauseApp(){

}

public void destroyApp(boolean unconditional) {

}

Page 18: Laporan Praktikum 7 Persistence

public void commandAction(Command c, Displayable d){

if (c== exitCommand){

destroyApp(true);

notifyDestroyed();// Exit

}

if (c== newCommand){

try{

// Buka dan atau buatlah record store dengan nama “RmsFilter”

recStore= RecordStore.openRecordStore("RmsFilter", true);

ByteArrayOutputStream out = new ByteArrayOutputStream();

DataOutputStream dOut= new DataOutputStream(out);

//Menyimpansebuahinteger

dOut.writeInt(recStore.getNextRecordID()* recStore.getNextRecordID());

// Menyimpan sebuah string

dOut.writeUTF("Record #" + recStore.getNextRecordID());

byte[] bytes = out.toByteArray();

// Menuliskan Record pada Store

recStore.addRecord(bytes,0,bytes.length);

dOut.close();

out.close();

recStore.closeRecordStore();

}catch(Exception e){

System.out.println(e.toString());

}

dispRec();

}

}

public void dispRec(){

recList.deleteAll();

try{

// Membuka atau membuat sebuah record store dengan nama "RmsFilter"

Page 19: Laporan Praktikum 7 Persistence

recStore= RecordStore.openRecordStore("RmsFilter", true);

// Mengambil isi dari record store

RecordEnumeration enumerator

= recStore.enumerateRecords(this, this, false);

while (enumerator.hasNextElement()){

//MenujuRecordselanjutnya

byte[] recBytes = enumerator.nextRecord();

ByteArrayInputStream in = new ByteArrayInputStream(recBytes);

DataInputStream dIn = new DataInputStream(in);

intcount=dIn.readInt();

String item = dIn.readUTF();

recList.append(count+","+item,null);

dIn.close();

in.close();

}

recStore.closeRecordStore();

}catch (Exception e){

}

}

public int compare(byte[] rec1, byte[] rec2){

String record1 = new String(rec1).toUpperCase();

String record2 = new String(rec2).toUpperCase();

//Sorting Ascending

if (record1.compareTo(record2) < 0){

return(PRECEDES);

} else{

if (record1.compareTo(record2) > 0){

return(FOLLOWS);

} else{

return(EQUIVALENT);

}

Page 20: Laporan Praktikum 7 Persistence

}

}

public boolean matches(byte[] candidate){

boolean isaMatch = false;

try{

ByteArrayInputStream bin = new ByteArrayInputStream(candidate);

DataInputStream dIn = new DataInputStream(bin);

intcount=dIn.readInt();

String item = dIn.readUTF();

// mendapatkan record dengan akhiran “0”

if (item.endsWith("0")){

isaMatch=true;

} else{

isaMatch=false;

}

}catch (Exception e){recList.append(e.toString(), null); }

return(isaMatch);

}

}

Page 21: Laporan Praktikum 7 Persistence

2.6 LATIHAN

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import javax.microedition.rms.*;

import java.io.*;

public class RmsPrefs implements RecordFilter {

String searchName = null;

Page 22: Laporan Praktikum 7 Persistence

public boolean matches(byte[] candidate){

boolean isaMatch = false;

try {

ByteArrayInputStream bIn = new ByteArrayInputStream(candidate);

DataInputStream dIn = new DataInputStream(bIn);

String item = dIn.readUTF();

if (searchName != null){

if (searchName.indexOf(item) != -1){

isaMatch = true;

}

}

dIn.close();

bIn.close();

} catch (Exception e){}

return(isaMatch);

}

public String readVar(RecordStore recStore, String name, String defaultValue){

String value = defaultValue;

searchName = name;

try {

// Load contents of Record Store

RecordEnumeration enumerator = recStore.enumerateRecords(this, null, false);

// get only the first matching record

if (enumerator.hasNextElement()){

// Get the next record

byte[] recBytes = enumerator.nextRecord();

ByteArrayInputStream in = new ByteArrayInputStream(recBytes);

DataInputStream dIn = new DataInputStream(in);

String sname = dIn.readUTF();

value = dIn.readUTF();

dIn.close();

Page 23: Laporan Praktikum 7 Persistence

in.close();

}

} catch (Exception e){}

return(value);

}

public void writeString(RecordStore recStore, String name, String value){

try {

ByteArrayOutputStream out = new ByteArrayOutputStream();

DataOutputStream dOut = new DataOutputStream(out);

// Store the name

dOut.writeUTF(name);

// Store the value

dOut.writeUTF(value);

byte[] bytes = out.toByteArray();

// Write the Record into the Store

recStore.addRecord(bytes, 0, bytes.length);

dOut.close();

out.close();

} catch (Exception e){}

}

}

Page 24: Laporan Praktikum 7 Persistence
Page 25: Laporan Praktikum 7 Persistence

BAB III

KESIMPULAN

3.1 Kesimpulan

Adapun kesimpulan yang dapat diperoleh berdasarkan hasil pembahasan dan praktikum, yaitu di

dalam Persistence adalah MIDP Record Management System adalah sebuah fasilitas yang

dimiliki oleh MIDlets untuk menyimpan data-data aplikasi pada saat MIDlet invocations. Data

akan disimpan dalam non-volatile memory didalam device. Hal ini berarti, data-data program

yang telah disimpan tidak akan hilang walaupun program di restart maupun device dimatikan.