In previous tutorial we learned how to create a simple recordstore.
In this tutorial we will also learn deletion and updation of records.
But keep 1 thing in mind , delete the topmost record only (it will also work otherwise ,but will break the sequence as the deleted record is left as a null pointer).
One more thing , when you run this code on toolkit ,it will remember the last data entered,to flush it,use clean and build option or change the recordstore name.
Code:-
import java.io.PrintStream;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreNotOpenException;
public class Midlet1 extends MIDlet implements CommandListener, Runnable
{
private Form form;
private Form form1;
private Form form2;
private Form form3;
private TextField t;
private TextField t1;
private TextField t2;
private TextField t3;
private Command write;
private Command read;
private Command back;
private Command delete;
private Command confirmd;
private Command confirmu;
private Command update;
private RecordStore recordStore;
private Alert alert;
public Midlet1()
{
form = new Form("Record");
form2 = new Form("Deletion Form");
form3 = new Form("Updation Form");
read = new Command("Read", 1, 1);
write = new Command("Write", 1, 1);
delete = new Command("Delete", 1, 1);
back = new Command("Back", 1, 1);
confirmd = new Command("Confirm Delete", 1, 1);
confirmu = new Command("Confirm Update", 1, 1);
update = new Command("Update Record", 1, 1);
form.addCommand(write);
form.addCommand(read);
form.addCommand(delete);
form.addCommand(update);
form2.addCommand(confirmd);
form2.addCommand(back);
form3.addCommand(back);
form3.addCommand(confirmu);
form.setCommandListener(this);
form2.setCommandListener(this);
form3.setCommandListener(this);
try{
recordStore = RecordStore.openRecordStore("My Record Store", true);
}
catch(Exception ex)
{ }
t = new TextField("Enter data", "", 30, 0);
t1 = new TextField("Enter record Id to be Deleted", "", 30, 0);
t2 = new TextField("Enter record Id to be Updated", "", 30, 0);
t3 = new TextField("Enter data", "", 30, 0);
form.append(t);
form2.append(t1);
form3.append(t2);
form3.append(t3);
}
public void startApp()
{
Display.getDisplay(this).setCurrent(form);
} public void pauseApp()
{
}
public void destroyApp(boolean flag)
{ }
public void commandAction(Command c, Displayable d)
{
if(c == write)
try{
byte bytestream[] = t.getString().getBytes();
int i = recordStore.addRecord(bytestream, 0, bytestream.length);
Alert a = new Alert("Done", "Record Added",null, null);
a.setTimeout(1000);
Display.getDisplay(this).setCurrent(a);
}
catch(Exception ex) { }
if(c == back)
Display.getDisplay(this).setCurrent(form);
if(c == read)
{
Thread th = new Thread(this);
th.start();
}
if(c == update)
try{
Display.getDisplay(this).setCurrent(form3);
}
catch(Exception ex)
{
ex.printStackTrace();
}
if(c == confirmu)
try{
recordStore.setRecord(Integer.parseInt(t2.getString()), t3.getString().getBytes(), 0, t3.size());
Alert a1 = new Alert("Done", "Record Updated",null, null);
a1.setTimeout(1000);
Display.getDisplay(this).setCurrent(a1);
}
catch(Exception ex)
{
ex.printStackTrace();
}
if(c == delete)
try
{
Display.getDisplay(this).setCurrent(form2);
}
catch(Exception ex)
{
ex.printStackTrace();
}
if(c == confirmd)
try
{
recordStore.deleteRecord(Integer.parseInt(this.t1.getString()));
Display.getDisplay(this).setCurrent(form2);
Alert a2 = new Alert("Done", "Record Deleted",null, null);
a2.setTimeout(1000);
Display.getDisplay(this).setCurrent(a2);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public void run()
{
byte byteInputData[] = new byte[1];
int length = 0;
try
{
for(int x = 0; x <= recordStore.getNumRecords(); x++)
try
{
int timeout=1000;
if(recordStore.getRecordSize(x) > byteInputData.length)
byteInputData = new byte[recordStore.getRecordSize(x)];
length = recordStore.getRecord(x, byteInputData, 0);
alert = new Alert("Reading","RecordID:- "+ x + " " +"Data:- "+ new String(byteInputData, 0, length), null, AlertType.WARNING);
alert.setTimeout(timeout);
Display.getDisplay(this).setCurrent(alert);
Thread.sleep(timeout);
}
catch(Exception error) { }
}
catch(RecordStoreNotOpenException ex)
{
ex.printStackTrace();
}
Display.getDisplay(this).setCurrent(form);
}
}
In this tutorial we will also learn deletion and updation of records.
But keep 1 thing in mind , delete the topmost record only (it will also work otherwise ,but will break the sequence as the deleted record is left as a null pointer).
One more thing , when you run this code on toolkit ,it will remember the last data entered,to flush it,use clean and build option or change the recordstore name.
Code:-
import java.io.PrintStream;
import javax.microedition.lcdui.*;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreNotOpenException;
public class Midlet1 extends MIDlet implements CommandListener, Runnable
{
private Form form;
private Form form1;
private Form form2;
private Form form3;
private TextField t;
private TextField t1;
private TextField t2;
private TextField t3;
private Command write;
private Command read;
private Command back;
private Command delete;
private Command confirmd;
private Command confirmu;
private Command update;
private RecordStore recordStore;
private Alert alert;
public Midlet1()
{
form = new Form("Record");
form2 = new Form("Deletion Form");
form3 = new Form("Updation Form");
read = new Command("Read", 1, 1);
write = new Command("Write", 1, 1);
delete = new Command("Delete", 1, 1);
back = new Command("Back", 1, 1);
confirmd = new Command("Confirm Delete", 1, 1);
confirmu = new Command("Confirm Update", 1, 1);
update = new Command("Update Record", 1, 1);
form.addCommand(write);
form.addCommand(read);
form.addCommand(delete);
form.addCommand(update);
form2.addCommand(confirmd);
form2.addCommand(back);
form3.addCommand(back);
form3.addCommand(confirmu);
form.setCommandListener(this);
form2.setCommandListener(this);
form3.setCommandListener(this);
try{
recordStore = RecordStore.openRecordStore("My Record Store", true);
}
catch(Exception ex)
{ }
t = new TextField("Enter data", "", 30, 0);
t1 = new TextField("Enter record Id to be Deleted", "", 30, 0);
t2 = new TextField("Enter record Id to be Updated", "", 30, 0);
t3 = new TextField("Enter data", "", 30, 0);
form.append(t);
form2.append(t1);
form3.append(t2);
form3.append(t3);
}
public void startApp()
{
Display.getDisplay(this).setCurrent(form);
} public void pauseApp()
{
}
public void destroyApp(boolean flag)
{ }
public void commandAction(Command c, Displayable d)
{
if(c == write)
try{
byte bytestream[] = t.getString().getBytes();
int i = recordStore.addRecord(bytestream, 0, bytestream.length);
Alert a = new Alert("Done", "Record Added",null, null);
a.setTimeout(1000);
Display.getDisplay(this).setCurrent(a);
}
catch(Exception ex) { }
if(c == back)
Display.getDisplay(this).setCurrent(form);
if(c == read)
{
Thread th = new Thread(this);
th.start();
}
if(c == update)
try{
Display.getDisplay(this).setCurrent(form3);
}
catch(Exception ex)
{
ex.printStackTrace();
}
if(c == confirmu)
try{
recordStore.setRecord(Integer.parseInt(t2.getString()), t3.getString().getBytes(), 0, t3.size());
Alert a1 = new Alert("Done", "Record Updated",null, null);
a1.setTimeout(1000);
Display.getDisplay(this).setCurrent(a1);
}
catch(Exception ex)
{
ex.printStackTrace();
}
if(c == delete)
try
{
Display.getDisplay(this).setCurrent(form2);
}
catch(Exception ex)
{
ex.printStackTrace();
}
if(c == confirmd)
try
{
recordStore.deleteRecord(Integer.parseInt(this.t1.getString()));
Display.getDisplay(this).setCurrent(form2);
Alert a2 = new Alert("Done", "Record Deleted",null, null);
a2.setTimeout(1000);
Display.getDisplay(this).setCurrent(a2);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public void run()
{
byte byteInputData[] = new byte[1];
int length = 0;
try
{
for(int x = 0; x <= recordStore.getNumRecords(); x++)
try
{
int timeout=1000;
if(recordStore.getRecordSize(x) > byteInputData.length)
byteInputData = new byte[recordStore.getRecordSize(x)];
length = recordStore.getRecord(x, byteInputData, 0);
alert = new Alert("Reading","RecordID:- "+ x + " " +"Data:- "+ new String(byteInputData, 0, length), null, AlertType.WARNING);
alert.setTimeout(timeout);
Display.getDisplay(this).setCurrent(alert);
Thread.sleep(timeout);
}
catch(Exception error) { }
}
catch(RecordStoreNotOpenException ex)
{
ex.printStackTrace();
}
Display.getDisplay(this).setCurrent(form);
}
}
No comments:
Post a Comment