Using the RecordStore class
Written by: maffelu , 2009-07-18 21:36:09
| Id | Bytes |
|---|---|
| 1 | 0101 |
| 2 | 101101011001 |
| 3 | 0001 |
package org.morkalork.recordStore;
import javax.microedition.lcdui.*;
/** * Displays a Form where you can enter a contacts * name and phone number. * @author maffelu */
public class ContactForm extends Form{
//==================FIELDS===================
private TextField txtName;
private TextField txtPhone;
private Command cmdAdd;
private Command cmdExit;
private Command cmdReset;
private Command cmdShow;
//==================PROPERTIES===============
/** * Returns an add command * @return A Command object */
public Command getCmdAdd(){
if(cmdAdd == null){
cmdAdd = new Command("Add", Command.SCREEN, 0);
}
return cmdAdd;
}
/** * Returns an exit command * @return A Command object */
public Command getCmdExit(){
if(cmdExit == null){
cmdExit = new Command("Exit", Command.EXIT, 0);
}
return cmdExit;
}
/** * Returns a reset command * @return A Command object */
public Command getCmdReset(){
if(cmdReset == null){
cmdReset = new Command("Reset form", Command.OK, 1);
}
return cmdReset;
}
/** * Returns a show command * @return A Command object */
public Command getCmdShow(){
if(cmdShow == null){
cmdShow = new Command("Show contacts", Command.OK, 1);
}
return cmdShow;
}
/** * Returns a TextField object * @return a TextField object */
public TextField getTxtName(){
if(txtName == null){
txtName = new TextField("Name: ", "", 45, TextField.ANY);
}
return txtName;
}
/** * Returns a TextField object * @return a TextField object */
public TextField getTxtPhone(){
if(txtPhone == null){
txtPhone = new TextField("Phone: ", "", 25, TextField.ANY);
}
return txtPhone;
}
//=====================CONSTRUCTOR=========================
/** * Creates the form * @param title The Form object title * @param parent A parent to set as command listener */
public ContactForm(String title, RecordStoreMIDlet parent){
super(title);
this.append(getTxtName());
this.append(getTxtPhone());
this.addCommand(getCmdAdd());
this.addCommand(getCmdReset());
this.addCommand(getCmdShow());
this.addCommand(getCmdExit());
this.setCommandListener(parent);
}
//======================METHODS============================
/** * Reset the form controls */
public void reset(){
this.txtName.setString("");
this.txtPhone.setString("");
}
}
package org.morkalork.recordStore;
import javax.microedition.rms.*;
import javax.microedition.midlet.*;
/** * A class to handle the record store * @author maffelu */
public class DBManager {
//======================FIELDS=====================
RecordStore db = null;
String fileName;
//======================CONSTRUCTOR================
/** * Constructor * @param fileName The name of the database */
public DBManager(String fileName){
this.fileName = fileName;
}
//======================METHODS====================
/** * Safely open the database */
public void openDB(){
try{
db = RecordStore.openRecordStore(fileName, true);
} catch (RecordStoreException rse){
System.err.println(rse.getMessage());
}
}
/** * Safely close the database */
public void closeDB(){
try{
//If recordstore is empty, remove it
if(db.getNumRecords() == 0){
String fName = db.getName();
db.closeRecordStore();
db.deleteRecordStore(fName);
} else {
db.closeRecordStore();
}
} catch (RecordStoreException rse){
System.err.println(rse.getMessage());
}
}
/** * Insert data into the database * @param record The data to input * @return int The new record id */
public synchronized int insert(String record){
try{
//Open database connection
this.openDB();
//Convert the string to bytes
byte[] data = record.getBytes();
//Insert the byte array and return the new record id
return db.addRecord(data, 0, data.length);
} catch (RecordStoreException rse){
System.err.println(rse.getMessage());
} finally {
//Close database connection
this.closeDB();
}
//If failed, return -1
return -1;
}
/** * Reads the database into an array and returns it * @return A string array containing the records from the database */
public String[] readDB(){
//Initialize an empty array
String[] data = null;
try{
//Open a connection to the database
this.openDB();
//Create an array
data = new String[db.getNumRecords()];
//Loop through the records and add them to the array
for(int i = 1; i <= db.getNumRecords(); i++){
byte[] record = db.getRecord(i);
data[i - 1] = new String(record);
}
} catch (Exception e){
System.err.println(e.getMessage());
} finally {
//Close database connection
this.closeDB();
}
//If something went wrong, return the nulled string array
return data;
}
/** * Returns the number of records in the database * @return An int representing the number of records in the database */
public int getNumberOfRecords(){
try{
//Open database connection
this.openDB();
//Return the number of records
return db.getNumRecords();
} catch (RecordStoreNotOpenException rsnoe){
System.err.println(rsnoe.getMessage());
} finally {
//Close database connection
this.closeDB();
}
//If something went wrong, return 0
return 0;
}
}
package org.morkalork.recordStore;
import java.io.IOException;
import javax.microedition.lcdui.*;
/** * A screen to display the contacts in the database * @author maffelu */
public class DisplayContacts extends Canvas{
//===============FIELDS=========================
private Image image;
private Command cmdBack;
private DBManager dbm;
private String[] contacts;
//===============PROPERTIES=====================
/** * Returns a back command * @return A command object */
public Command getCmdBack(){
if(cmdBack == null){
cmdBack = new Command("Back", Command.EXIT, 0);
}
return cmdBack;
}
/** * Returns a database managing object * @return A DBManager object */
public DBManager getDBManager(){
if(dbm == null){
dbm = new DBManager("MorkaDB");
}
return dbm;
}
//===================CONSTRUCTOR=======================
/** * constructor, creates an image object */
public DisplayContacts(RecordStoreMIDlet parent, String[] contacts) {
this.contacts = contacts;
//Get image
try{
image = Image.createImage("/org/morkalork/recordStore/images/contactsBackground.jpg");
} catch (IOException ioe){
System.err.println(ioe.getMessage());
}
cmdBack = getCmdBack();
this.addCommand(cmdBack);
this.setCommandListener(parent);
}
//==================METHODS===========================
/** * paint */
public void paint(Graphics g) {
//Set background image
g.setGrayScale(255);
g.fillRect(0, 0, g.getClipWidth() -1, g.getClipHeight() -1);
g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
//Output the contacts
g.setGrayScale(0);
//Only attempt to output contacts if the contacts array is really an array
if(contacts.getClass().isArray()){
int delimiter; //We used a comma ',' as delimiter in the database
String name; //Will hold the contact name
String phone; //Will hold the contact phone number
int top = 60; //The distance to draw from the top (60 covering the title)
int left = 5; //The distance to draw from the left, wont' change
for(int i = 0; i < contacts.length; i++){
//Get contact data
delimiter = contacts[ i].indexOf(",");
name = contacts[ i].substring(0, delimiter);
phone = contacts[ i].substring(delimiter + 1, contacts[ i].length());
//Output it
g.drawString("Name: " + name, left, top, Graphics.TOP | Graphics.LEFT);
top = top + 12; //Add a linebreak
g.drawString("Phone: " + phone, left, top, Graphics.TOP | Graphics.LEFT);
top = top + 24; //Add a linebreak (12) and an extra break (12)
//between this and the next record.
}
} else {
System.err.println("Contacts is not an array...");
}
}
}
package org.morkalork.recordStore;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
/** * A phone book * @author maffelu */
public class RecordStoreMIDlet extends MIDlet implements CommandListener{
//====================FIELDS=============================
private ContactForm contactForm;
private DisplayContacts displayContacts;
private DBManager dbm;
//===================PROPERTIES==========================
/** * Returns a ContactForm object * @return a ContactForm object */
public ContactForm getContactForm(){
if(contactForm == null){
contactForm = new ContactForm("Contact form", this);
}
return contactForm;
}
/** * Returns a database manager object * @return a DBManager object */
public DBManager getDBManager(){
if(dbm == null){
dbm = new DBManager("MorkaDB");
}
return dbm;
}
//===================CONSTRUCTOR=========================
/** * Constructor */
public RecordStoreMIDlet(){
contactForm = getContactForm();
dbm = getDBManager();
}
//===================METHODS=============================
/** * Returns a Display object * @return a Display object */
public Display getDisplay(){
return Display.getDisplay(this);
}
/** * Switches the current screen to a new screen * @param alert An Alert screen, or NULL * @param displayable The new display object */
public void switchDisplay(Alert alert, Displayable displayable){
Display display = getDisplay();
if(alert == null){
display.setCurrent(displayable);
} else {
display.setCurrent(alert, displayable);
}
}
public void commandAction(Command command, Displayable displayable){
if(displayable == contactForm){
if(command == contactForm.getCmdAdd()){
String record = contactForm.getTxtName().getString();
record += ",";
record += contactForm.getTxtPhone().getString();
dbm.insert(record);
contactForm.reset();
} else if(command == contactForm.getCmdShow()){
String[] data = dbm.readDB();
switchDisplay(null, new DisplayContacts(this, data));
} else if(command == contactForm.getCmdReset()){
contactForm.reset();
} else if(command == contactForm.getCmdExit()){
exitMIDlet();
}
} else if(displayable == displayContacts){
if(command == displayContacts.getCmdBack()){
switchDisplay(null, contactForm);
}
}
}
public void startMIDlet(){
switchDisplay(null, getContactForm());
}
public void exitMIDlet(){
destroyApp(true);
notifyDestroyed();
}
public void startApp() {
startMIDlet();
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
}
There are no comments on this article.
If you have any question or just want to leave a message, just fill out the form below!
Your e-mail will not be visible in your post, it is for validation reasons only
Maffelu
Creator and admin of MorkaLork.com.
Started programming in HTML back when frames and tables was the way to design a page, moved on to Pascal/Delphi, PHP, javascript/jQuery, VB.NET/C#, Java and C++.
Currently studies .NET (in general) focusing on ASP.NET.