Search This Blog

Monday, 25 September 2023

Experiment-10: Storing and Retrieving Records from a Record Store

0 comments

Storing and Retrieving Mixed Types of Data

  • It is common for records to consist of mixed data types such as string, boolean, and as shown below:
  • Suppose we want to store the customer name, customer number, and gender. A string is used to store a customer name, an integer to store the customer number, and a boolean to indicate gender.
  • Two streams are used to write the record to the record store. 
    • The first stream is an object of ByteArrayOutputStream 
    • The other stream is an object of type DataOutputStream, which is used to output the byte array output stream
  • The DataOutputStream class has methods that write specific data types to a buffer. They are writeUTF() method, writeBoolean() method, and writeInt() method. Each is passed the appropriate data.
  • The objective is to write data to a buffer, then write the buffered data to the stream.  This is done by calling the flush() method.
  • The data stream is then converted to a byte array. The byte array is then written to the record store.  This process is done by calling the toByteArray() method, which returns a reference to the byte array of the stream.

Steps to Follow to Store Data in to the Store:

    1. Create an object of ByteArrayOutputStream by calling the ByteArrayOutputStream() constructor
    2. Create an object of DataOutputStream by calling the DataOutputStream() constructor and passing it reference to the byte array output stream.
    3. Call the write methods of the DataOutputStream object to store data in the buffer.
    4. Then the buffered data is placed in the data stream by calling the flush() method. 
    5. The stream is converted to a byte array by calling the toByteArray() method, which returns a reference to the byte array of the stream. 
    6. The reference to the byte array is passed to the addRecord() method, which saves the byte array as a new record in the record store. 
    7. Call the reset() method of the ByteArrayOutputStream object to clear the internal store of the object 
    8. Next, close the output stream and the data output stream objects. 
    9. Any errors occurring while the data is being written to the record store are trapped by the catch {} block and displayed in an alert dialog box.

    Steps to Read Data from a Data Store:

    • Reading a mixed data type record from a record store is similar to the routine that writes mixed data types. 
    • First, you create an instance of the ByteArrayInputStream class.
    • The constructor of this class is passed the byte array that was just created. 
    • Also create an instance of the DataInputStream class and pass reference to the ByteArrayInputStream class to the DataInputStream class constructor.
    • The routine used to read records from a record store must assume that more than one record exists, and therefore you need to include a for loop so the MIDlet continues to read records from a record store until the last record is read.

    MIDlet that Writes and Reads Mixed Types of Data:

    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");
        byte[] outputRecord;
    String outputString = "First Record";
    int outputInteger = 15;
    boolean outputBoolean = true;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    DataOutputStream outputDataStream =
    new DataOutputStream(outputStream);
    outputDataStream.writeUTF(outputString);
    outputDataStream.writeBoolean(outputBoolean);
    outputDataStream.writeInt(outputInteger);
    outputDataStream.flush();
    outputRecord = outputStream.toByteArray();
    recordstore.addRecord(outputRecord, 0, outputRecord.length);
    outputStream.reset();
    outputStream.close();
    outputDataStream.close();
        
        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
        String inputString = null;
    int inputInteger = 0;
    boolean inputBoolean = false;
    byte[] byteInputData = new byte[100];
    ByteArrayInputStream inputStream = new ByteArrayInputStream(byteInputData);
    DataInputStream inputDataStream =
    new DataInputStream(inputStream);
    for (int x = 1; x <= recordstore.getNumRecords(); x++)
    {
    recordstore.getRecord(x, byteInputData, 0);
    inputString = inputDataStream.readUTF();
    inputBoolean = inputDataStream.readBoolean();
    inputInteger = inputDataStream.readInt();
    inputStream.reset();
    }
    inputStream.close();
    inputDataStream.close();
    alert = new Alert("Reading", inputString + " " + inputInteger + " " + inputBoolean, null, AlertType.WARNING);
    alert.setTimeout(Alert.FOREVER);
    display.setCurrent(alert);
    }
    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