Hi how to create a mobile book on j2me program, for example
dictionary, bible, magazine…. then read on the mobile phone,
tell me the code and steps or other help site. Thanks

Answers were Sorted based on User's Feedback



Hi how to create a mobile book on j2me program, for example dictionary, bible, magazine…. then ..

Answer / mohit chaudhry

package hello;
import javax.microedition.rms.*;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

public class ReadWrite extends MIDlet implements CommandListener,RecordListener
{
private RecordStore rs = null;
RecordEnumeration re=null;
Display disp;
Alert a1,a2;
List l1,l2;
Form f,f1;
TextField tf,tf1;
Command edit,delete,back,go,ok;
public ReadWrite()
{
disp=Display.getDisplay(this);
a1=new Alert("Result");
a2=new Alert("Listener Result");
edit=new Command("Edit",Command.SCREEN,1);
delete=new Command("Delete",Command.SCREEN,2);
back=new Command("Back",Command.BACK,3);
ok=new Command("Ok",Command.OK,1);
go=new Command("Go",Command.OK,1);
l1=new List("Main Screen",List.IMPLICIT);
l2=new List("Contacts",List.IMPLICIT);
l1.append("Read", null);
l1.append("Write", null);
l1.append("Exit", null);
l1.addCommand(ok);
l2.addCommand(delete);
l2.addCommand(edit);
l2.addCommand(back);
l1.setCommandListener(this);
l2.setCommandListener(this);
f=new Form("Add Record");
tf=new TextField("Enter Data","",50,TextField.ANY);
f.append(tf);
f.addCommand(go);
f.setCommandListener(this);
try
{
rs = RecordStore.openRecordStore("data", true );

rs.addRecordListener(this);
}
catch (Exception e)
{ }
}
public void commandAction(Command c,Displayable d)
{
String s=l1.getString(l1.getSelectedIndex());
if(c==ok && s.equals("Read"))
{
l2.deleteAll();
try
{
re= rs.enumerateRecords(null, null, true);
while( re.hasNextElement() ){
byte[] data = re.nextRecord();
l2.append(new String(data), null);
}
disp.setCurrent(l2);
}
catch (Exception e)
{ }
}
if(c==ok && s.equals("Write"))
{
tf.setString(" ");
disp.setCurrent(f);
}
if(c==ok && s.equals("Exit"))
{
destroyApp(false);
notifyDestroyed();
try
{
rs.closeRecordStore();
RecordStore.deleteRecordStore("db");
}
catch (Exception e)
{ }
}
if(c==delete)
{
try
{
re = rs.enumerateRecords(null, null, true);
if (re.hasNextElement())
{
int recordId = re.nextRecordId();
rs.deleteRecord(recordId);

}
}
catch (Exception ex)
{ }
}
if(c==edit)
{
disp.setCurrent(f1);

}

if(c==back)
{
disp.setCurrent(l1);
}
if(c==go)
{
byte[] rec = tf.getString().getBytes();
try
{
rs.addRecord(rec, 0, rec.length);
tf.setString("");
}
catch (Exception e)
{ }

}

}

public void destroyApp( boolean unconditional )
{}

public void startApp()
{
disp.setCurrent(l1);
}

public void pauseApp()
{ }

public void recordAdded(RecordStore r,int rid)
{

a2.setString("record listener called for record add");
disp.setCurrent(a2,l1);
}

public void recordDeleted(RecordStore r,int rid)
{

a2.setString("record listener called for record delete");
disp.setCurrent(a2,l1);
}
public void recordChanged(RecordStore r,int rid)
{

a2.setString("record listener called for record chsnge");
disp.setCurrent(a2,l1);
}
}

Is This Answer Correct ?    12 Yes 3 No

Hi how to create a mobile book on j2me program, for example dictionary, bible, magazine…. then ..

Answer / binukjames

Following is a simple J2ME Record store program.
Create a project(MIDet) using this code and see the console
output.
The same way u can store / update ur data into RMS(Record
Management Systen _mobile database). create a user interface
for this program if required.

//===RmsListener.java ============================START

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

public class RmsListener extends MIDlet
{
private RecordStore rs = null;
static final String REC_STORE = "db_8";

public RmsListener()
{
// Open record store and add listener
openRecStore();
rs.addRecordListener(new TestRecordListener());

// Initiate actions that will wake up the listener
writeRecord("J2ME and MIDP");
updateRecord("MIDP and J2ME");
deleteRecord();

closeRecStore(); // Close record store
deleteRecStore(); // Remove the record store
}

public void destroyApp( boolean unconditional )
{
}

public void startApp()
{
// There is no user interface, go ahead and shutdown
destroyApp(false);
notifyDestroyed();
}

public void pauseApp()
{
}

public void openRecStore()
{
try
{
// The second parameter indicates that the record store
// should be created if it does not exist
rs = RecordStore.openRecordStore(REC_STORE, true);
}
catch (Exception e)
{
db(e.toString());
}
}

public void closeRecStore()
{
try
{
rs.closeRecordStore();
}
catch (Exception e)
{
db(e.toString());
}
}

public void deleteRecStore()
{
if (RecordStore.listRecordStores() != null)
{
try
{
RecordStore.deleteRecordStore(REC_STORE);
}
catch (Exception e)
{
db(e.toString());
}
}
}

public void writeRecord(String str)
{
byte[] rec = str.getBytes();

try
{
rs.addRecord(rec, 0, rec.length);
}
catch (Exception e)
{
db(e.toString());
}
}

public void updateRecord(String str)
{
try
{
rs.setRecord(1, str.getBytes(), 0, str.length());
}
catch (Exception e)
{
db(e.toString());
}
}

public void deleteRecord()
{
try
{
rs.deleteRecord(1);
}
catch (Exception e)
{
db(e.toString());
}
}

/*--------------------------------------------------
* Simple message to console for debug/errors
* When used with Exceptions we should handle the
* error in a more appropriate manner.
*-------------------------------------------------*/
public void db(String str)
{
System.err.println("Msg: " + str);
}

}


/*--------------------------------------------------
* Listen for updates to the record store
*-------------------------------------------------*/
class TestRecordListener implements RecordListener
{
public void recordAdded(RecordStore recordStore, int
recordId)
{
try
{
System.out.println("Record with ID#: " + recordId +
" added to RecordStore: " +
recordStore.getName());
}
catch (Exception e)
{
System.err.println(e);
}
}

public void recordDeleted(RecordStore recordStore, int
recordId)
{
try
{
System.out.println("Record with ID#: " + recordId +
" deleted from RecordStore: " +
recordStore.getName());
}
catch (Exception e)
{
System.err.println(e);
}
}

public void recordChanged(RecordStore recordStore, int
recordId)
{
try
{
System.out.println("Record with ID#: " + recordId +
" changed in RecordStore: " +
recordStore.getName());
}
catch (Exception e)
{
System.err.println(e);
}
}
}
//=====================================================END

Is This Answer Correct ?    8 Yes 6 No

Post New Answer

More J2ME Interview Questions

What is profile ?

2 Answers  


What is RMS ?

3 Answers  


currently runnaning the application how can calucate the size of the application?

1 Answers  


Describe and explain WML ?

1 Answers  


What is cHTML ?

1 Answers  






What is WAP Gateway ?

2 Answers  


create a menu which has the following options:cut-can be on/off,copy-can be on/off,paste-can be on/off,delete-can be on/off,select all-put all4 options on,unselect all-put all 4 options off,using event handling in MIDP application

0 Answers  


what is the difference between jad and manifest file?

6 Answers   Microsoft,


What is JDBC for CDC/FP ?

1 Answers  


i am working in blackberry.In blackberry emulator it is connecting to server but when i installed the program in blackberry it is not connecting to server why

3 Answers  


Write a Java program to deactivate a cell phone when authentication failed

0 Answers  


What is UMTS

1 Answers  


Categories