Animan!acs

4 May 2006

Managing RMS in J2ME

Filed under: Sains

Ini sedikit source code tentang mengelola database pake RMS di J2ME, ya lumayan membantu,
tapi kalo ada kekurangan tolong dikoreksi  emoticon

pertama buat field apa aja yang akan disimpan dalam satu recordstore:
 
public class TypeSettingRecord  //   
    {
        public String Type;
        public int Interval;
        public String Password;           
    }
public RecordStore rsSetting=null;
public RecordEnumeration reSetting=null;
public String TypeRefresh="";
public int  Interval =0;
public String Pass="";
 
trus buat untuk penyimpanan/edit:
 
public boolean saveuser(String Type,int IntervalRefresh,String Passwd, boolean update)
   {
       if (Passwd=="")
       {
           Passwd= Pass;
       }
       if (type=="")
       {
           type = TypeRefresh;
       }
       if (intervalRefresh==0)
       {
           IntervalRefresh=Interval;
       }
       TypeSettingRecord record = new TypeSettingRecord();
            record.Type=Type;
           record.Interval=IntervalRefresh;
           record.Password=Passwd;

        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        DataOutputStream os=new DataOutputStream(baos);           
        try
        {
            rsSetting = RecordStore.openRecordStore("setting",true);         
        }
        catch (RecordStoreException rse)
        {           
            alert("RSE Save Setting Error","Failed error code"+ rse, AlertType.ERROR, Alert.FOREVER, MainMenu);  //panggil procedure/class alert yang bertugas menampilkan Alert
            return false;
        }
        try
        {           
                os.writeUTF(record.Type);
                os.writeInt(record.Interval);// <– penulisan disesuaikan dengan type data. bisa dipelajari dengan tipe2 yang laen. contoh: os.writeBoolean, writeChar, writeByte ……
                os.writeUTF(record.Password);
                os.close();
        }
        catch (IOException ioe)
        {
            alert("IOException Save Setting Error","Failed error code"+ ioe, AlertType.ERROR, Alert.FOREVER, MainMenu);       
            return false;
        }
        byte[] data=baos.toByteArray();
        try
        {
            if (update)
            {
                rsSetting.setRecord(rsSetting.getNextRecordID()-1,data,0 ,data.length);                //jika melakukan update data
            }
            else
            {
                rsSetting.addRecord(data,0,data.length);               //menyimpan data baru
            }
        }
        catch (RecordStoreException rse)
        {
           alert("RSE Save Setting Error","Failed error code"+ rse, AlertType.ERROR, Alert.FOREVER, MainMenu);  //panggil procedure/class alert yang bertugas menampilkan Alert
            return false;
        }        
        return true;
    }
 
=====
kalo delete bisa dibuat :
 
public void deletesetting(int RecId)
    {
        RecordStore rsSetting=null;
        RecordEnumeration reSetting=null        
try
        {
            rsSetting= RecordStore.openRecordStore("setting",false);
            reSetting= rsSetting.enumerateRecords(null,null,true);   
            rsSetting.deleteRecord(RecId);
        }
        catch (RecordStoreException rse)
        {
            alert.setString("Failed error code"+ rse);
            display.setCurrent(alert);            
           
        }
         finally
        {
            try
            {
                    reSetting.destroy();
                    reSetting=null
                    rsSetting.closeRecordStore();
                    reSetting=null;
            }
            catch (RecordStoreException rse){
        }
        }
       
    }
============
Baca data:

public  void getsetting(int RecId)
{
     try
        {
            rsSetting=RecordStore.openRecordStore("setting",true);
            reSetting=rsSetting.enumerateRecords(null,null,true);               
            if (reSetting.hasNextElement())
            {
                int id=reSetting.nextRecordId();
                ByteArrayInputStream bais=new ByteArrayInputStream(RecId);
                DataInputStream is=new DataInputStream(bais);     
                try
                {
                    TypeRefresh=is.readUTF();
                    Interval = is.readInt();  // ini juga disesuaikan dengan tipe data /// ada readBoolean, readChar …. bisa dipelajari
                    Pass=is.readUTF();
                }
                catch (IOException eofe)
                {
                }
            }
        }
         catch(RecordStoreException rse)
         {
         }
     finally {
        try
        {
                reSetting.destroy();
                rsSetting.closeRecordStore();
                rsSetting=null;
                reSetting=null;
        }
        catch (RecordStoreException rse){
        }
     }
}
 
==============
pemakaiannya:

- save :
if (saveuser("A", 5000, "aan", false))
{
 ……………
……………
}
-update :
if (savesetting("A", 1000, "Aan Isnaini", true)); // bisa dibuat sesuai kebutuhan apa yang akan di update
{
…………….
……………….
}
- delete :
deletesetting(10); //isi record id yang akan dihapus.






















Get free blog up and running in minutes with Blogsome
Theme designed by Hadley Wickham