Search This Blog

Monday, 25 September 2023

Experiment-9: Writing and Reading String Data from a Record Store

0 comments

Storing and Retrieving Records from a Record Store

  • Once a MIDlet opens a record store, the MIDlet can write records to the record store and read information already stored there 
  • Two techniques available for writing and reading records. 
    • The first technique is used to write and read a string of data and is used primarily whenever we have one data column in the record store such as a list of abbreviations of states. 
    • The second technique is used to write and read multiple columns of data of different types such as string, integer, and boolean
  • The addRecord() method is used to write a record to the record store. 
  • The addRecord() method requires three parameters. 
    • The first is a byte array containing the byte value of the string being written to the record store. 
    • The second is an integer representing the index of the first byte of the byte array that is to be written to the record store. 
    • The third is the total number of bytes that is to be written to the record store.

Steps to Follow to Store Records:

  • The first step in writing a string to a record store is to create an instance of a String and assign text to the instance. 
  • Next, the string must be converted to a byte array by calling the getBytes() method, as shown here. 
  • The getBytes() method returns a byte array.
string.getBytes()
  • The second parameter of the addRecord() method is usually zero
  • The third parameter is the length of the byte array, indicating that the entire byte array should be written to the record store.

Steps to Follow to Read Records:

  • Typically, information is read from a record store a record at a time and stored in a byte array.
  • The byte array is then converted to a string, which is then displayed on the screen or processed further based on the needs of the application.
  • To know the number of records in a record store, call the method getNumRecords() method of the RecordStore class.  It returns an integer that represents the total number of records in the record store.
  • Use the value returned by getNumRecords() to iterate through all the records in a for loop.  Call the getRecord() method of the RecordStore class during each iteration of the for loop.
  • The getRecord() method returns bytes from the RecordStore, which are stored in a byte array that you create. The getRecord() method requires three parameters:
    • The first parameter is the record ID 
    • The second parameter is the byte array that you create for storing the record. 
    • The third parameter is an integer representing the position in the record from which to begin copying into the byte array.
  • The following code segment reads the second record from the record store and copies that record, beginning with the first byte of the record, from the record store into the byte array:
recordstore.getRecord(2, myByteArray, 0)
  • Typically, the first parameter of the getRecord() method is the integer of the for loop, and the third parameter is zero, indicating the entire record is to be copied into the byte array.

MIDlet that Stores and Retrieves Records from Record Store:


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

class RecordStoreMidlet extends MIDlet implements CommandListener
{
private Display display;
private Alert alert;
private Form form;
private Command exit;
private Command openStore;
private Command newStore;
private Command clsStore;
private Command delStore;
private RecordStore recordstore = null;
private RecordEnumeration recordenumeration = null;
private StringItem st;
private TextField txtStoreName;

public RecordStoreMidlet ()
{
display = Display.getDisplay(this);
st = new StringItem("","");
txtStoreName = new TextField("Name of the Store", "", 31, 0);
openStore = new Command("Open Store", Command.SCREEN, 1);
newStore = new Command("Create Store", Command.SCREEN, 2);
exit = new Command("Exit App", Command.SCREEN, 5);
form = new Form("Record Store");
form.append(txtStoreName);
form.addCommand(openStore);
form.addCommand(newStore);
form.addCommand(exit);
form.setCommandListener(this);
}

public void startApp()
{
display.setCurrent(form);
}

public void pauseApp()
{
}

public void destroyApp( boolean unconditional )
{
}

public void commandAction(Command command, Displayable displayable)
{
if (command == exit)
{
destroyApp(true);
notifyDestroyed();
}
else if (command == newStore)
{
try
{
    recordstore = RecordStore.openRecordStore(txtStoreName.getString(), true );
    st.setText("The Record Store has been Created and Record has been Added");
    
    String outputData = "Hello Students of MRECW!";
byte[] byteOutputData = outputData.getBytes();
recordstore.addRecord(byteOutputData, 0, byteOutputData.length);
    
    form.append(st);
}
catch (Exception error)
{
alert = new Alert("File Status!", error.toString(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}
}
else if (command == openStore)
{
try
{
    recordstore = RecordStore.openRecordStore(txtStoreName.getString(), false );
    st.setText("The Record Store Exists and Open for Storage and Retrieval");
    //Code required for Retrieving Record from Record Store
    byte[] byteInputData = new byte[1];
    int length = 0;
    if (recordstore.getRecordSize(1) > byteInputData.length)
{
byteInputData = new byte[recordstore.getRecordSize(1)];
}
length = recordstore.getRecord(1, byteInputData, 0);

    alert = new Alert("Reading", new String(byteInputData, 0, length), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
    form.append(st);
}
catch (Exception error)
{
    st.setText("The Record Store Does not Exist.  Create a New One!");
alert = new Alert("File Status!", st.getText(), null, AlertType.WARNING);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
form.append(st);
}
}
}
}

Leave a Reply