Page 3 of 6
The MIDP API offers many methods to manipulate stores and records. The following method opens a record store:
RecordStore noteStore = RecordStore.openRecordStore("JWTestStore", true);
The first parameter in openRecordStore() is a name that uniquely identifies the record store. The second parameter indicates whether or not a new store should be
created if the named store does not exist.
To add a new record containing the string note to the record store, we use the addRecord() method:
noteStore.addRecord( note.getBytes(), 0, note.length() );
After we gather a few notes, we can send them to the database using SendData. In the MIDlet SendData, we transverse all the records in a store using the enumeration object and then delete the processed records:
RecordEnumeration re = noteStore.enumerateRecords(null, null, false);
while ( re.hasNextElement() ) {
int id = re.nextRecordId();
byte [] b = noteStore.getRecord(id);
// Do something about the data in array b.
// Assemble it into the XML document to be send to server.
noteStore.deleteRecord(id);
}
The first parameter in method enumerateRecords() is a RecordFilter, which decides which records to include in the returned enumeration. The second parameter is a RecordComparator, which specifies the order in which the records are returned. In the above example, we used the null value for both parameters, so the enumeration contains all records in the store in no particular order. The third parameter
indicates whether the enumeration should be kept up-to-date with changes in the record store.
The above name-based record-store access methods are convenient. However, there is a problem: you can install multiple MIDlets
suites on the same device, and chances are that some suites might choose to use the same record store names. To avoid that
confusion, the MIDP RecordStore class has a built-in security model. Any MIDlet in a suite can access only RMS databases created by MIDlets in the same suite.
The static method RecordStore.listRecordStores() returns an array of record store names accessible to the current MIDlet.
In this section, we look at how to establish data connections on both the client and the server side. On the MIDP client side, open a new connection to a specific URL with the following code:
HttpConnection conn = (HttpConnection) Connector.open(url);
To send data over HTTP, use conn.setRequestMethod(HttpConnection.POST) for the POST method or conn.setRequestMethod(HttpConnection.GET) for the GET method.
Now we can send data to the server using the output stream from the connection object:
DataOutputStream os = conn.openDataOutputStream(); os.write(StringToPost.getBytes());
Finally, we can retrieve the server response data from the input stream:
DataInputStream is = conn.openDataInputStream();
On the JSP server side, the client's request data can be retrieved in the input stream reader:
Reader reader = request.getReader();
The server talks with the database through JDBC connections (Do not confuse JDBC connection objects with HTTP connection objects!):