Home > J2ME, Java, RMS > Simple Class for RMS in Java Micro Edition

Simple Class for RMS in Java Micro Edition


package DbLokal;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 *
 * @author Ratno Kustiawan
 */
public class tb {
    protected RecordStore rs;
    protected RecordEnumeration re;
    protected ByteArrayOutputStream baos;
    protected ByteArrayInputStream bais;
    protected DataOutputStream dos;
    protected DataInputStream dis;
    protected String name;
    protected String[] field,arrTemp;
    protected String[][] temp,temp2;
    protected int iCounter,r,j,len1,len2;

    protected byte[] b;
    protected String[][] back;
    protected int idx;
    protected int nextId;
    protected int colCount;
    public tb(String name,int colCount) {
       this.name=name;
       this.colCount=colCount;
    }
    public boolean Connect() {
        try {            
            rs = RecordStore.openRecordStore(name, true);
            return true;
        } catch (RecordStoreException ex) {
            ex.printStackTrace();        
            return false;
        }
    }
    
    public boolean IsNew(){
        try {
            rs = RecordStore.openRecordStore(name, true);
            re=rs.enumerateRecords(null, null, false);
            if (re.numRecords()<1) 
                return true;
            else 
                return false;
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
            return true;
        }
    }
    public boolean update(String keyWord,int colNumber,String newValue){
        arrTemp=new String[colCount];
        try {
            re = rs.enumerateRecords(null, null, false);
            while(re.hasNextElement()){
                idx=re.nextRecordId();
                b=rs.getRecord(idx);  
                bais=new ByteArrayInputStream(b);
                dis=new DataInputStream(bais); 
                String found=dis.readUTF();
                if(found.equals(keyWord)){
                    arrTemp[0]=found;
                    for(iCounter=1;iCounter<colCount;iCounter++){
                        arrTemp[iCounter]=dis.readUTF();
                    }
                    if(Delete(keyWord)){
                        arrTemp[colNumber]=newValue;
                        return Insert(arrTemp);
                    }
                }
            }            
            return false;
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
            return false;
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
            return false;
        } 
    }
    public String[][] Select(int data) {
        try {
            re = rs.enumerateRecords(null, null, false);
            back=new String[re.numRecords()][data];
            r=0;
            len1=data;
            
            while(re.hasNextElement()){
                idx=re.nextRecordId();
                b=rs.getRecord(idx);  
                bais=new ByteArrayInputStream(b);
                dis=new DataInputStream(bais); 
                for(iCounter=0;iCounter<data;iCounter++){
                    back[r][iCounter]=dis.readUTF();
                }
                r++;
            }
            return back;
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
            return null;
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
            return null;
        } catch (IOException ex) {
            ex.printStackTrace();
            return null;        
        }
    }
    public int Max(){
        temp=Select(1);
        if(temp.length<=0)
            return 1;
        int max=Integer.parseInt(temp[0][0]);
        int ii;
        int value=max;
        for(ii=1;iimax)
                max= value;
        }
        return max+1;
    }
    public String[][] Select() {
        return this.Select(colCount);
    }
    
    public boolean IsExist(String data){        
        temp=Select(1);
        for(iCounter=0;iCounter<temp.length;iCounter++){
            if(temp[iCounter][0].equals(data))
                return true;            
        }
        return false;
    }
    public boolean IsExist(String[] data){  
       
        temp=Select(1);        
        for(iCounter=0;iCounter<temp.length;iCounter++){
            for(j=0;j<data.length;j++){
                if(temp[iCounter][j].equals(data[j]))
                    return true;            
            }
        }
        return false;
    }
    public boolean Insert(String dataInsert) {       
       return Insert(new String[]{dataInsert});       
    }
    public int Count(){
        try {
            re = rs.enumerateRecords(null, null, false);
            return re.numRecords();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
            return 0;
        }
    }

    public int getColCount() {
        return colCount;
    }
    protected boolean status;
    public boolean Insert(String[] arrDataInsert) {        
        try {
            baos = new ByteArrayOutputStream();
            dos = new DataOutputStream(baos);
            len1 = arrDataInsert.length;
            for (iCounter = 0; iCounter < len1; iCounter++) {
                try {
                    dos.writeUTF(arrDataInsert[iCounter]); 
                } catch (IOException ex) {
                    ex.printStackTrace();
                    return false;
                }
            }            
            b = baos.toByteArray();
            rs.addRecord(b, 0, b.length);
            return true;
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
            return false;
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
            return false;
        }
    }
    public boolean  Delete(String keyWord){
        try {
            re = rs.enumerateRecords(null, null, false); 
            while(re.hasNextElement()){                
                idx=re.nextRecordId();
                b=rs.getRecord(idx);   
                bais=new ByteArrayInputStream(b);
                dis=new DataInputStream(bais); 
                String sementara=dis.readUTF();

                if(sementara.trim().equals(keyWord)){
                    rs.deleteRecord(idx);  
                }
                
            }
            return true;
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
            return false;
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
            return false;

        } catch (IOException ex) {
            ex.printStackTrace();
            return false;        
        }
    }
    
    public boolean  Delete(String keyWord,String delimeter){
        try {
            re = rs.enumerateRecords(null, null, false); 
            while(re.hasNextElement()){                
                idx=re.nextRecordId();
                b=rs.getRecord(idx);                   
                
                bais=new ByteArrayInputStream(b);
                dis=new DataInputStream(bais); 
                String sementara=dis.readUTF();
                if(sementara.trim().substring(0, sementara.indexOf(delimeter.trim())).equals(keyWord)){
                    rs.deleteRecord(idx);                    
                }
                
            }
            return true;
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
            return false;
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
            return false;
        } catch (IOException ex) {
            ex.printStackTrace();
            return false;        
        }
    }
    public boolean Drop(){        
        try {
            re = rs.enumerateRecords(null, null, false); 
            while(re.hasNextElement()){
                idx=re.nextRecordId();
                b=rs.getRecord(idx);                   
                rs.deleteRecord(idx);
            }
            return true;
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
            return false;
        } catch (RecordStoreException ex) {
            ex.printStackTrace();
            return false;
        } 
    }
    
}
Categories: J2ME, Java, RMS Tags: , , ,
  1. wahyu widayanto
    August 20, 2011 at 7:36 am

    Mas mau tanya
    ane lagi bingun ngitung penambahan tanggal di j2me.
    ane rencana pake datefield yg merupakan komponen tanggal dij2me.

    ane sedang bikin aplikasi penghitung masa kesuburan dg proses :

    1. tanggal awal menstruasi : ……
    2. lama Siklus menstruasi : …….
    3 Menghitung masa sbur =
    tanggal awal mens + lama siklus
    examp:
    Jan, 17 2011 + 18 hari = 3 Pebruari 2011
    jadi masa subur anda pada 3 pebruari 2011

    minta bantuanny mas gimana ane bisa menjumlahkan tanggal menjadi bilangan kemudian ane bisa mendapatkan sebuah tanggal sesuai dengan tanggal dikalender yang nyata.

    trim…

    Wahyu W
    ubeibaki412@gmail.com

  1. No trackbacks yet.

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

%d bloggers like this: