Solved

J2ME RMS(Record Management System)

Posted on 2004-10-17
14
535 Views
Last Modified: 2013-12-03
hi, wanna ask if someone could code out a sample code whereby it can store names and phone numbers into the RMS. Thank you.
0
Comment
Question by:bluelagoon88
  • 6
  • 6
14 Comments
 
LVL 13

Expert Comment

by:petmagdy
ID: 12337740
Here We go an example a game that stores top achieved scores, each record is (Name|#trials|Level#) example (John|13|2):



import javax.microedition.rms.*;
import java.io.*;
import java.util.*;

public class MMStore
{
//Single Tone Class
    protected RecordStore m_TopScores;    
   
    /** Creates a new instance of MMStore */
    public MMStore() throws Exception
    {
        m_TopScores = RecordStore.openRecordStore("MasterMindTS",true);
    }
   
   
    public void close() throws Exception
    {
        if(!m_bOpened)
        {
            return;
        }
        m_TopScores.closeRecordStore();
    }
   
 
     protected static byte[] constructRecord(Vector elements, char cSeparator)
    {
        String szRecord="";
        int nSize= elements.size();
        for(int i=0; i < nSize ; i++)
        {
            szRecord += (String) elements.elementAt(i);
            if( i < (nSize - 1))
            {    
                String sep = String.valueOf(cSeparator);
                szRecord += sep;
            }
        }
//System.out.println("Constructed szRecord: " + szRecord);      
       
        return szRecord.getBytes();
    }
   
   
   
    public int getTopScoresRecordsCount() throws RecordStoreNotOpenException
    {
        return m_TopScores.getNumRecords();
    }
   
   
   
   
    public Integer addTopScore(String szName, int nTrials) throws Exception
    {
        Vector elements = new Vector();
        int nLevel=2;
        elements.addElement(szName);
        elements.addElement(String.valueOf(nTrials));
        elements.addElement(String.valueOf(nLevel));
        byte[] record = constructRecord(elements,'|');
        int nRecID=m_TopScores.addRecord(record,0, record.length);
        return new Integer(nRecID);
    }
   
    public void removeAllScores() throws Exception
    {
        Hashtable topScores = new Hashtable();
//System.out.println("Start MMStore.getTopScores()");        
        RecordEnumeration enum = m_TopScores.enumerateRecords(null,null,false);
//System.out.println("enumerateRecords MMStore.getTopScores()");        
       
        if(enum == null)
        {
            return;
        }
        while(enum.hasNextElement())
        {
            int nRecId=enum.nextRecordId();
            m_TopScores.deleteRecord(nRecId);
        }
    }
 
}

0
 

Author Comment

by:bluelagoon88
ID: 12353783
petmagdy, can this code be modified so that it can be use to store only the names and numbers in a handphone? Currently doin a project that needs to store people's names and contacts into the handphone(RMS). Thanks in advance.
0
 

Author Comment

by:bluelagoon88
ID: 12354464
by the way.. i need to compile it as a MIDlet
Using Wireless toolkit to compile. Thanks again.
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12355865
You can store any thing u want it is not structured u are just as if u r storing rows, each row is a byte[], so u construct a string by cocatinating a name + '|' + handphone number, i am using '|' character as a separator u can choose something else, to construct ur MIDP application and compile and assemble ur application as JAD and JAR easily I recommend for u Sun Java studio Mobility 6 u can download it from

http://wwws.sun.com/software/download/products/41085de1.html

good luck
0
 

Author Comment

by:bluelagoon88
ID: 12366248
petmagdy, theres an error in the coding.. may i know what is the problem?


C:\WTK20\apps\MMStore\src\MMStore.java:19: cannot resolve symbol
symbol  : variable m_bOpened
location: class MMStore
        if(!m_bOpened)

            ^
1 error
com.sun.kvem.ktools.ExecutionException
Build failed
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12366958
yes put under the line

    protected RecordStore m_TopScores;    

this line to define m_bOpened

protected boolean m_bOpened;

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:bluelagoon88
ID: 12368273
ok thanks.. i will see wat i can do..
0
 

Author Comment

by:bluelagoon88
ID: 12368853
theres an error in this set of code... wats the problem with it?

--------------------------------------------------------------------------------------------------

import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import java.io.*;
public class RecordStoreExample
      extends MIDlet implements CommandListener
{
  private Display display;
  private Alert alert;
  private Form form;
  private Command exit;
  private Command start;
  private RecordStore recordstore = null;
  private RecordEnumeration recordenumeration = null;
public RecordStoreExample ()
{
  display = Display.getDisplay(this);
  exit = new Command("Exit", Command.SCREEN, 1);
  start = new Command("Start", Command.SCREEN, 1);
  form = new Form("Record Store");
  form.addCommand(exit);
  form.addCommand(start);
  form.setCommandListener(this);
}
public void startApp()
{
  display.setCurrent(form);
}
public void pauseApp()
{
}
public void destoryApp (boolean unconditional)
{
}
public void commandAction(Command command, Displayable displayable)
{
  if (Command == exit)
{
  destoryApp(true);
  notifyDestoryed();
}
else if (command == start)
{
  try
  {
    recordstore = RecordStore.openRecordStore("myRecordStore", true);
  }
  catch (Exception error)
  {
    alert = new Alert("Error Creating", error.toString(), null, AlertType.Warning);
    alert.setTimeout(Alert.FOREVER);
    display.setCurrent(alert);
  }
  if (RecordStore.listRecordStores() != null)
  {
  try
    {
      RecordStore.deleteRecordStore("myRecordStore");
    }
  catch (Exception error)
  {
   alert = new Alert("Error Removing", error.toString(), null, AlertType.WARNING);
   alert.setTimeout(Alert.FOREVER);
   display.setCurrent(alert);
  }
  }
  }
 }
}

----------------------------------------------------------------------------------------------

Here's the error message:
c:\WTK104\apps\RecordStoreExample\src\RecordStoreExample.java:5: RecordStoreExample is not abstract and does not override abstract method destroyApp(boolean) in javax.microedition.midlet.MIDlet

public class RecordStoreExample

       ^

c:\WTK104\apps\RecordStoreExample\src\RecordStoreExample.java:37: cannot resolve symbol

symbol  : variable Command

location: class RecordStoreExample

  if (Command == exit)

      ^

c:\WTK104\apps\RecordStoreExample\src\RecordStoreExample.java:40: cannot resolve symbol

symbol  : method notifyDestoryed ()

location: class RecordStoreExample

  notifyDestoryed();

  ^

c:\WTK104\apps\RecordStoreExample\src\RecordStoreExample.java:50: cannot resolve symbol

symbol  : variable Warning

location: class javax.microedition.lcdui.AlertType

    alert = new Alert("Error Creating", error.toString(), null, AlertType.Warning);

                                                                         ^

4 errors
0
 
LVL 13

Accepted Solution

by:
petmagdy earned 125 total points
ID: 12368923
add this function to ur class

public void destroyApp(boolean unconditional)
{
}

please be case sensetive
0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12368933
Also in the line

  if (Command == exit)


change it to:

  if (command == exit)

0
 
LVL 13

Expert Comment

by:petmagdy
ID: 12368969
sorry also u wrote this line wrongly:

  notifyDestoryed();

write it:


  notifyDestroyed();

Also change

    alert = new Alert("Error Creating", error.toString(), null, AlertType.Warning);

to

    alert = new Alert("Error Creating", error.toString(), null, AlertType.WARNING);

again be case sensitive
0
 

Author Comment

by:bluelagoon88
ID: 12397147
ok thx... i'll try it out
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 71
Base1 Encode/Decode 3 68
Windows 10 IE Certificate Issue 10 42
What are programming codes and how do they work? 7 74
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
A short article about problems I had with the new location API and permissions in Marshmallow
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
The viewer will learn how to implement Singleton Design Pattern in Java.

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now