import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.InvalidRecordIDException;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;
public class RecordIDMIDlet extends MIDlet implements CommandListener {
private Command exitCommand= new Command("Exit", Command.EXIT, 1);
private Display display;
public RecordIDMIDlet() {
display = Display.getDisplay(this);
}
public void startApp() {
TextBox aTextBox = new TextBox("Main", null, 256, TextField.ANY);
RecordStore rs = null;
byte[] name = null;
boolean existingOrNot = false;
boolean OK = true;
existingOrNot = existing("aRS");
if (existingOrNot) {
try {
rs = RecordStore.openRecordStore("aRS", false);
} catch (Exception e) {
OK = false;
} finally {
if (OK) {
aTextBox.setString("Ok");
} else {
aTextBox.setString("Failed");
}
}
} else {
try {
rs = RecordStore.openRecordStore("aRS", true);
} catch (Exception e) {
OK = false;
} finally {
if (OK) {
aTextBox.setString("Ok");
} else {
aTextBox.setString("Failed");
}
}
}
if (OK) {
try {
for (int i = 1; i <= 3; i++) {
name = ("Name " + i).getBytes();
rs.addRecord(name, 0, name.length);
}
} catch (Exception e) {
aTextBox.setString("Add Falied");
}
try {
rs.deleteRecord(1);
aTextBox.setString("recordID Delete");
} catch (InvalidRecordIDException e) {
try {
aTextBox.setString(rs.getNumRecords() + "recordID");
} catch (RecordStoreNotOpenException rse) {
}
} catch (RecordStoreException e) {
} finally {
try {
rs.closeRecordStore();
} catch (Exception e) {
}
}
}
aTextBox.addCommand(exitCommand);
aTextBox.setCommandListener(this);
display.setCurrent(aTextBox);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
public boolean existing(String recordStoreName) {
boolean existingOrNot = false;
RecordStore rs = null;
if (recordStoreName.length() > 32)
return false;
try {
rs = RecordStore.openRecordStore(recordStoreName, false);
} catch (RecordStoreNotFoundException e) {
existingOrNot = false;
} catch (Exception e) {
} finally {
try {
rs.closeRecordStore();
} catch (Exception e) {
}
}
return existingOrNot;
}
public void commandAction(Command c, Displayable s) {
destroyApp(false);
notifyDestroyed();
}
}
|