Simple RMS J2ME

Posted: November 28, 2010 in J2ME
Tags: , , , ,

Sample Resource Management System (RMS) untuk menyimpan data pada aplikasi Mobile J2ME


import java.io.ByteArrayInputStream;

import java.io.ByteArrayOutputStream;

import java.io.DataInputStream;

import java.io.DataOutputStream;

import java.io.IOException;

import javax.microedition.midlet.*;

import javax.microedition.lcdui.*;

import javax.microedition.rms.*<!--more-->;

public class CRUD_BarKode extends MIDlet  implements CommandListener {

    //deklarasi

  private Display display;

  private Form form,banding;

  private RecordStore rs;

  private RecordEnumeration re;

  private ChoiceGroup choicegroup;

  private Alert alert;

  private List list;

  //untuk proses entri data

  private Form entri;

  private TextField tfNama, tfHarga,tfID_BC,tfKeterangan,tfOld_Id, input1, input2,input3, input4, input5;

  //construktor

  private final Command cmdKeluar =    new Command("Keluar", Command.EXIT, 1);

  private final Command cmdPilih =    new Command("Pilih", Command.OK, 1);

  private final Command cmdSimpan =    new Command("Simpan", Command.SCREEN, 1);

  private final Command cmdUpdate =    new Command("Ubah", Command.SCREEN, 1);

  private final Command cmdHapus =    new Command("Hapus", Command.SCREEN, 1);

  private final Command cmdEdit=    new Command("Edit", Command.SCREEN, 1);

  private final Command cmdKembali =    new Command("Kembali", Command.BACK, 1);

  private final Command combanding =    new Command("bandingkan", Command.OK, 1);

  //instance class

  public CRUD_BarKode() {

    display = Display.getDisplay(this);

    alert = new Alert(null);

    alert.setTimeout(Alert.FOREVER);

    list = new List(null, Choice.IMPLICIT);

    rs = null;

    // membuat atau membuka record store

    try

    {

        rs = RecordStore.openRecordStore("db_KodeBar", true);

    }

    catch (RecordStoreException rse) //klo error = klo exception

    {

        alert.setString("Record store tidak dapat dibuka. " +"Aplikasi akan dihetikan");

        alert.setType(AlertType.ERROR);

        display.setCurrent(alert, null);

        System.exit(1);

    }

  }

  public void startApp()

  {

      tambahRecord("0123456789012","Kursi","200000","The overall goal of this book is to provide introductory coverage of Symbian OS");

      tambahRecord("9789797635862","Komputer","20000000","The overall goal of this book is to provide introductory coverage of Symbian OS");

      tambahRecord("9789797635862","Lemari","2000000","The overall goal of this book is to provide introductory coverage of Symbian OS");

      tambahRecord("9789791101349","Meja","200000","The overall goal of this book is to provide introductory coverage of Symbian OS");

      form = new Form("Inventarisasi Barang/ Produk");

      choicegroup = new ChoiceGroup("Menu Inventarisasi :", Choice.EXCLUSIVE);

      choicegroup.append("Tambah Data ", null);

      choicegroup.append("Lihat Data ", null);

      form.append(choicegroup);

      form.addCommand(cmdKeluar);

      form.addCommand(cmdPilih);

      form.setCommandListener(this);

      display.setCurrent(form);

  }

  public void pauseApp() {

  }

  public void destroyApp(boolean unconditional) {

  }

  public void commandAction(Command c, Displayable s)

  {

      if (c == cmdKeluar)

      {

          destroyApp(false);

          notifyDestroyed();

      }

      else if (c == cmdPilih)

      {

          switch (choicegroup.getSelectedIndex())

          {

              case 0:

              {

                  entriData(); //fungsi masukin data

                  break;

              }

              case 1:

              {

                  lihatRecord(); //fungsi liat record

                  break;

              }

          }

      }

      else if (c == cmdKembali)

      {

          display.setCurrent(form);

      }

      else if (c == cmdSimpan)

      {

          alert.setType(AlertType.INFO);

          if (!tfNama.equals("") && !tfHarga.equals("") && !tfID_BC.equals("") && !tfKeterangan.equals("")) //cek jika var ada isi nya

          {

              tambahRecord(tfID_BC.getString(),tfNama.getString(), tfHarga.getString(),  tfKeterangan.getString());

              alert.setString("Data baru telah berhasil disimpan");

              display.setCurrent(alert, form);

          }

          else //jika data kosong

          {

              alert.setString("Data Nama Produk, Harga, ID Brcode/ ISBN, Keterangan " + "tidak boleh kosong");

              display.setCurrent(alert, entri);

          }

      }

      else if (c == cmdUpdate)

      {

          alert.setType(AlertType.INFO);

          if (!tfNama.equals("") && !tfHarga.equals("") && !tfID_BC.equals("") && !tfKeterangan.equals("")) //cek jika var ada isi nya

          {

              updateRecord(tfID_BC.getString(),tfNama.getString(), tfHarga.getString(),  tfKeterangan.getString(),  tfOld_Id.getString());

              alert.setString("Data baru telah berhasil disimpan");

              display.setCurrent(alert, form);

          }

          else //jika data kosong

          {

              alert.setString("Data Nama Produk, Harga, ID Brcode/ ISBN, Keterangan " + "tidak boleh kosong");

              display.setCurrent(alert, entri);

          }

      }

      else if (c == cmdHapus || c == cmdEdit)

      {

          int pos = list.getString(list.getSelectedIndex()).indexOf("->"); //ambil index dari list

          String id_bc = list.getString(list.getSelectedIndex()).substring(0, pos);

          if(c == cmdHapus){

            hapusRecord(id_bc);

          }

          else{

            editData(id_bc);

          }

      }

  }

  //fungsi tambah record

  public void tambahRecord(String id_bc, String nama, String harga, String keterangan)

  {

    byte[] temp = null;

    try

    {

        //kosntruktor baos dan dos

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        DataOutputStream dos = new DataOutputStream(baos);

        //cari fungsi ini ByteArrayOutputStream dan DataOutputStream di internet

        dos.writeUTF(id_bc);// tulis var pada dos

        dos.writeUTF(nama);// tulis var pada dos

        dos.writeUTF(harga);

        dos.writeUTF(keterangan);

        temp = baos.toByteArray();

    }

    catch (IOException ioe)

    {

        ioe.printStackTrace();

    }

    try

    {

        rs.addRecord(temp, 0, temp.length); //masukan data pada RecordStore

    } catch (RecordStoreNotOpenException rsnoe) {

      rsnoe.printStackTrace();

    } catch (RecordStoreException rse) {

      rse.printStackTrace();

    }

  }

   void updateRecord(String id_bc, String nama, String harga, String keterangan, String Old_Id) {

        byte[] temp = null;

      try

      {

          re = rs.enumerateRecords(null, null, false);

          while (re.hasNextElement())

          {

              int i = re.nextRecordId();

              temp = rs.getRecord(i);

              ByteArrayInputStream bais = new ByteArrayInputStream(temp);

              DataInputStream dis = new DataInputStream(bais);

              try

              {

                  String vID_BC = dis.readUTF();

                  if (vID_BC.equals(Old_Id))

                  {

                      rs.deleteRecord(i);

                      break;

                  }

              }

              catch (IOException ioe) { ioe.printStackTrace();}

      }

      re.rebuild();

      }

      catch (RecordStoreNotOpenException rsnoe) {rsnoe.printStackTrace();}

      catch (RecordStoreException rse) {rse.printStackTrace();}

        try

        {

            //kosntruktor baos dan dos

            ByteArrayOutputStream baos = new ByteArrayOutputStream();

            DataOutputStream dos = new DataOutputStream(baos);

            //cari fungsi ini ByteArrayOutputStream dan DataOutputStream di internet

            dos.writeUTF(id_bc);// tulis var pada dos

            dos.writeUTF(nama);// tulis var pada dos

            dos.writeUTF(harga);

            dos.writeUTF(keterangan);

            temp = baos.toByteArray();

        }

        catch (IOException ioe)

        {

            ioe.printStackTrace();

        }

        try

        {

            rs.addRecord(temp, 0, temp.length); //masukan data pada RecordStore

        } catch (RecordStoreNotOpenException rsnoe) {

          rsnoe.printStackTrace();

        } catch (RecordStoreException rse) {

          rse.printStackTrace();

        }

    }

  public void lihatRecord() {

    byte[] temp = null;

    list.setTitle("Daftar Barang/ Produk");

    list.deleteAll();

    try

    {

        re = rs.enumerateRecords(null, null, false); //enumerasi record

        while (re.hasNextElement()) //jk elemen rs lbh dr satu

        {

            int i = re.nextRecordId();

            temp = rs.getRecord(i);

            ByteArrayInputStream bais = new ByteArrayInputStream(temp);

            DataInputStream dis = new DataInputStream(bais);

            try

            {

                String id_bc = dis.readUTF();

                String nama = dis.readUTF();

                String harga = dis.readUTF();

                String keterangan = dis.readUTF();

                list.append(id_bc + "-> \n  -"+nama + "\n  -Rp "+harga+"\n -"+keterangan+" \n", null);// tampilkan

            }

            catch (IOException ioe) {ioe.printStackTrace();}

      }

      list.addCommand(cmdKembali);

      list.addCommand(cmdHapus);

      list.addCommand(cmdEdit);

      list.setCommandListener(this);

      display.setCurrent(list);

    }

    catch (InvalidRecordIDException invID) {invID.printStackTrace();}

    catch (RecordStoreNotOpenException rsnoe) {rsnoe.printStackTrace();}

    catch (RecordStoreException rse) {rse.printStackTrace();}

  }

  public void hapusRecord(String id_bc)

  {

      byte[] temp = null;

      try

      {

          re = rs.enumerateRecords(null, null, false);

          while (re.hasNextElement())

          {

              int i = re.nextRecordId();

              temp = rs.getRecord(i);

              ByteArrayInputStream bais = new ByteArrayInputStream(temp);

              DataInputStream dis = new DataInputStream(bais);

              try

              {

                  String vID_BC = dis.readUTF();

                  if (vID_BC.equals(id_bc))

                  {

                      rs.deleteRecord(i);

                      break;

                  }

              }

              catch (IOException ioe) { ioe.printStackTrace();}

      }

      re.rebuild(); //memulai enumerasi baru, karena data telah berubah

      lihatRecord();

      }

      catch (RecordStoreNotOpenException rsnoe) {rsnoe.printStackTrace();}

      catch (RecordStoreException rse) {rse.printStackTrace();}

  }

  public Form entriData()

  {

    entri = new Form("Entri Data");

    tfID_BC = new TextField("ID Barcode / ISBN :", null, 25, TextField.ANY);

    tfNama = new TextField("Nama Barang/ Produk :", null, 25, TextField.ANY);

    tfHarga = new TextField("Harg a:", null, 15, TextField.NUMERIC);

    tfKeterangan = new TextField("Keterangan :", null, 100, TextField.ANY);

    entri.append(tfID_BC);

    entri.append(tfNama);

    entri.append(tfHarga);

    entri.append(tfKeterangan);

    entri.addCommand(cmdSimpan);

    entri.addCommand(cmdKembali);

    entri.setCommandListener(this);

    display.setCurrent(entri);

    return entri;

  }

  public Form editData(String Id_Old)

  {

    entri = new Form("Edit Data");

    tfOld_Id = new TextField("ID Barcode / ISBN (Lama):", Id_Old, 25, TextField.UNEDITABLE);

    tfID_BC = new TextField("ID Barcode / ISBN (Baru):", Id_Old, 25, TextField.ANY);

    tfNama = new TextField("Nama Barang/ Produk :", null, 25, TextField.ANY);

    tfHarga = new TextField("Harg a:", null, 15, TextField.NUMERIC);

    tfKeterangan = new TextField("Keterangan :", null, 100, TextField.ANY);

    entri.append(tfOld_Id);

    entri.append(tfID_BC);

    entri.append(tfNama);

    entri.append(tfHarga);

    entri.append(tfKeterangan);

    entri.addCommand(cmdUpdate);

    entri.addCommand(cmdKembali);

    entri.setCommandListener(this);

    display.setCurrent(entri);

    return entri;

  }

}

Printscreen

rms j2me

Download : cari di menu SourceCode😀

Comments
  1. Kiki Ahmadi says:

    mas, pake tag source code dong, biar kodingnya enak di lihat

    http://en.support.wordpress.com/code/posting-source-code/

  2. immanuel says:

    import javax.microedition.rms.*;

    waktu saya build…muncul eror pada tulisan diatas…
    ; illegal expected
    ada solusi?

  3. candra says:

    terimakasih banyak, saya sungguh beruntung dapat berkunjung kesini. aplikasinya berhasil diekesekusi dengan lancar🙂 sungguh terimakasih banyak mas😀

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s