Mega Code Archive

 
Categories / Java Tutorial / J2ME
 

Add records to RecordStore

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.RecordStore; import javax.microedition.rms.RecordStoreNotFoundException; public class addRecordMIDlet extends MIDlet implements CommandListener {   private Command exitCommand;   private Display display;   public addRecordMIDlet() {     display = Display.getDisplay(this);     exitCommand = new Command("exit", Command.SCREEN, 1);   }   public void startApp() {     TextBox aTextBox = new TextBox("a text", null, 256, TextField.ANY);     RecordStore rs = null;     byte[] nameEmail = 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("aRS Opened");         } else {           aTextBox.setString("aRS cannot open");         }       }     } else {       try {         rs = RecordStore.openRecordStore("aRS", true);       } catch (Exception e) {         OK = false;       } finally {         if (OK) {           aTextBox.setString("Ok");         } else {           aTextBox.setString("not ok");         }       }     }     if (OK)       try {         nameEmail = "login@yourname.net".getBytes();         rs.addRecord(nameEmail, 0, nameEmail.length);         aTextBox.setString("Added");       } catch (Exception e) {         aTextBox.setString("failed");       }     if (OK)       try {         rs.closeRecordStore();       } catch (Exception e) {       }     String result = new String(nameEmail);     int position = result.indexOf('=');     result = "Name:" + result.substring(0, position) + "\n" + "E-mail?"         + result.substring(position + 1, result.length());     aTextBox.setString(result);     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();   } }