Rabu, 16 Mei 2012

Membuat Aplikasi Database RMS dengan Java ME Netbeans


Address Book dgn Database RMS Java ME

Untuk membuat aplikasi Address Book di Java ME NetBeans dapat digunakan RMS (Record Management Store). Langkah-langkah pembuatannya sebagai berikut :

1. Buka Netbeans. Klik menu File-New Project. Pilih Categories : Java ME, Projects : Mobile Application. Klik Next. Pada dialog berikutnya, isikan Project Name : MobileAddressBook. Kemudian cek Set As Main Project dan hilangkan cek pada Create Hello Midlet. Klik Next. Klik Next. Klik Finish.


2. Pada Project TreeView klik kanan, pilih New – Visual Midlet. Kemudian akan muncul dialog, isikan Class Name : MBook, package : my.appaddressbook. Klik Finish. Akan ditampilkan MIDlet kosong pada Design. Dari Palette Displayables tambahkan Form, kemudian ubah namanya menjadi FMain. Kemudian pada Form FMain tambahkan 3 buah ItemCommand dan exitCommand dari Pallete Commands. Ubah name dari ItemCommand menjadi cmAdd, cmEdit, dan cmDelete. Kemudian ubah label-nya menjadi “Add”, “Edit” dan “Delete”. Setelah itu tarik garis dari MIDlet Started menuju FMain, kemudian dari FMain exitCommand tarik garis ke MIDlet Resumed.

3. Kemudian Palette Displayables tambahkan Form lagi, ubah namanya menjadi FEdit. Kemudian pada Form FEdit tambahkan 1 buah ItemCommand dan cancelCommand dari Pallete Commands. Ubah name dari ItemCommand menjadi cmSave. Kemudian ubah label-nya menjadi “Save”. Setelah itu tarik garis dari cmAdd menuju FEdit dan dari cmEdit menuju FEdit. Kemudian tarik garis dari cmSave menuju FMain dan dari cancelCommand menuju FMain.


4. Pada Screen Design akan ditampilkan Design dari FMain. Ubah title dari FMain menjadi “My Book”, kemudian tambahkan 1 buah ChoiceGroup. Ubah name-nya menjadi cName. Kemudian ubah property label dari ChoiceGroup tersebut menjadi “Name List”. Sehingga tampak seperti pada tampilan berikut ini :


5. Pada Screen Design pilihlah tampilan Design dari FEdit. Ubah title dari FEdit menjadi “Edit”, kemudian tambahkan 3 buah TextField. Ubah name-nya menjadi tName, tAddress, dan tPhone. Kemudian ubah property label dari TextField tersebut menjadi “Name”, “Address”, dan “Phone”. Kemudian property text-nya kosongkan semua, menjadi “”. Sehingga tampak seperti pada tampilan berikut ini :


6. Pada cmAdd, klik kanan pilih Go to Source. Pada Source akan muncul kode program dari method public void commandAction(), di dalamnya pada blok else if (command == okCommand) { } tambahkan kode program berikut :

if (command == cmAdd) {                                        
// write pre-action user code here
      switchDisplayable(null, getFEdit());                                          
      // write post-action user code here
      tName.setString("");
      tAddress.setString("");
      tPhone.setString("");
      searchName = "";
}

6. Lanjutkan untuk cmEdit, cmDelete, cmSave, cmCancel dan lainnya. Berikut ini kode program selengkapnya : 

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import javax.microedition.io.*;
import java.io.*;


public class MBook extends MIDlet implements CommandListener {

    private boolean midletPaused = false;
                     
    private Form FMain;
    private ChoiceGroup cName;
    private Form FEdit;
    RecordStore myDB;
    String searchName;
    public void startMIDlet() {                                     
        // write pre-action user code here
        try {
            myDB = RecordStore.openRecordStore("MyBook", true);
        } catch (Exception e) {

        }
        switchDisplayable(null, getFMain());                                       
        // write post-action user code here
    }
    public Form getFMain() {
        if (FMain == null) {                                
            // write pre-init user code here
            FMain = new Form("My Book", new Item[] { getCName() });                                   
            FMain.addCommand(getCmAdd());
            FMain.addCommand(getCmEdit());
            FMain.addCommand(getCmDelete());
            FMain.addCommand(getExitCommand());
            FMain.setCommandListener(this);                                 
            // write post-init user code here
            try {
                ByteArrayInputStream bais;
                DataInputStream dis;
                String in;

                RecordEnumeration renu = myDB.enumerateRecords(null, null, false);

                cName.deleteAll();
                while(renu.hasNextElement()) {
                    int recID = renu.nextRecordId();
                    bais = new ByteArrayInputStream(myDB.getRecord(recID));
                    dis = new DataInputStream(bais);
                    in = dis.readUTF();
                    cName.append(in, null);
                }

            } catch (Exception e) {
                System.out.println(e.toString());
            }
        }                        
        return FMain;
    }
    public void commandAction(Command command, Displayable displayable) {                                              
        // write pre-action user code here
        if (displayable == FEdit) {                                          
            if (command == cancelCommand) {                                        
                // write pre-action user code here
                switchDisplayable(null, getFMain());                                          
                // write post-action user code here
                try {
                    ByteArrayInputStream bais;
                    DataInputStream dis;
                    String in;

                    RecordEnumeration renu = myDB.enumerateRecords(null, null, false);

                    cName.deleteAll();

                    while(renu.hasNextElement()) {
                        int recID = renu.nextRecordId();
                        bais = new ByteArrayInputStream(myDB.getRecord(recID));
                        dis = new DataInputStream(bais);
                        in = dis.readUTF();
                        cName.append(in, null);
                    }
                } catch (Exception e) {
                    System.out.println(e.toString());
                }
            } else if (command == cmSave) {                                         
                // write pre-action user code here
                if (searchName.equals("")) {
                    try {
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        DataOutputStream dos = new DataOutputStream(baos);
                        dos.writeUTF(tName.getString());
                        dos.writeUTF(tAddress.getString());
                        dos.writeUTF(tPhone.getString());
                        byte[] b = baos.toByteArray();
                        myDB.addRecord(b, 0, b.length);
                        baos.close();
                        dos.close();
                    } catch (Exception e) {
                        System.out.println(e.toString());
                    }
                } else {
                    try {
                        ByteArrayInputStream bais;
                        DataInputStream dis;
                        String strName;

                        RecordEnumeration renu = myDB.enumerateRecords(null, null, false);

                        while(renu.hasNextElement()) {
                            int recID = renu.nextRecordId();
                            bais = new ByteArrayInputStream(myDB.getRecord(recID));
                            dis = new DataInputStream(bais);
                            strName = dis.readUTF();
                            if (strName.equals(searchName)) {
                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                DataOutputStream dos = new DataOutputStream(baos);
                                dos.writeUTF(tName.getString());
                                dos.writeUTF(tAddress.getString());
                                dos.writeUTF(tPhone.getString());
                                byte[] b = baos.toByteArray();
                                myDB.setRecord(recID, b, 0, b.length);
                                baos.close();
                                dos.close();

                                break;
                            }
                        }
                    } catch (Exception e) {
                        System.out.println(e.toString());
                    }
                }
                switchDisplayable(null, getFMain());                                          
                // write post-action user code here
                try {
                    ByteArrayInputStream bais;
                    DataInputStream dis;
                    String in;

                    RecordEnumeration renu = myDB.enumerateRecords(null, null, false);

                    cName.deleteAll();

                    while(renu.hasNextElement()) {
                        int recID = renu.nextRecordId();
                        bais = new ByteArrayInputStream(myDB.getRecord(recID));
                        dis = new DataInputStream(bais);
                        in = dis.readUTF();
                        cName.append(in, null);
                    }
                } catch (Exception e) {
                    System.out.println(e.toString());
                }
            }                                          
        } else if (displayable == FMain) {
            if (command == cmAdd) {                                        
                // write pre-action user code here
                switchDisplayable(null, getFEdit());                                          
                // write post-action user code here
                tName.setString("");
                tAddress.setString("");
                tPhone.setString("");
                searchName = "";
            } else if (command == cmDelete) {                                         
                // write pre-action user code here
                searchName = cName.getString(cName.getSelectedIndex());

                try {
                    ByteArrayInputStream bais;
                    DataInputStream dis;
                    String strName;

                    RecordEnumeration renu = myDB.enumerateRecords(null, null, false);

                    while(renu.hasNextElement()) {
                        int recID = renu.nextRecordId();
                        bais = new ByteArrayInputStream(myDB.getRecord(recID));
                        dis = new DataInputStream(bais);
                        strName = dis.readUTF();
                        if (strName.equals(searchName)) {
                            myDB.deleteRecord(recID);
                            break;
                        }
                    }
                } catch (Exception e) {
                    System.out.println(e.toString());
                }
                                          
                // write post-action user code here
                try {
                    ByteArrayInputStream bais;
                    DataInputStream dis;
                    String in;

                    RecordEnumeration renu = myDB.enumerateRecords(null, null, false);

                    cName.deleteAll();

                    while(renu.hasNextElement()) {
                        int recID = renu.nextRecordId();
                        bais = new ByteArrayInputStream(myDB.getRecord(recID));
                        dis = new DataInputStream(bais);
                        in = dis.readUTF();
                        cName.append(in, null);
                    }
                } catch (Exception e) {
                    System.out.println(e.toString());
                }
            } else if (command == cmEdit) {                                         
                // write pre-action user code here
                switchDisplayable(null, getFEdit());                                           
                // write post-action user code here
                searchName = cName.getString(cName.getSelectedIndex());

                try {
                    ByteArrayInputStream bais;
                    DataInputStream dis;
                    String strName, strAddress, strPhone;

                    RecordEnumeration renu = myDB.enumerateRecords(null, null, false);

                    cName.deleteAll();
                    while(renu.hasNextElement()) {
                        int recID = renu.nextRecordId();
                        bais = new ByteArrayInputStream(myDB.getRecord(recID));
                        dis = new DataInputStream(bais);
                        strName = dis.readUTF();
                        strAddress = dis.readUTF();
                        strPhone = dis.readUTF();
                        if (strName.equals(searchName)) {
                            tName.setString(strName);
                            tAddress.setString(strAddress);
                            tPhone.setString(strPhone);
                        }
                    }
                } catch (Exception e) {
                    System.out.println(e.toString());
                }
            } else if (command == exitCommand) {                                          
                // write pre-action user code here
                exitMIDlet();                                           
                // write post-action user code here
            }                                                  
        }                                                
        // write post-action user code here
    }  
...
}                            

5. Klik Run untuk menjalankan aplikasi. Kemudian coba untuk menambah data, menampilkan data, mengedit data, dan menghapus data. Akan tampil aplikasi seperti di bawah ini :