Professional Documents
Culture Documents
J2ME. Mnh lp topic ny mong anh em hy chia s nhng g mnh hiu chng ta u
pht trin.
//==============================================================
============//
Th nht : Bn ghi ca J2Me(RecordStore) gm 2 trng nh hnh bn di :
Gi chng ta hy Xem xt v nh gi v d di y s r .
V d :
To 1 Form cha user,pass , vi 2 nt ng k, ng nhp.
khi nhp ni dung vo ta s nhn vo ng k ng k.
Sau khi ng k ri d liu s t ng c lu vo bn ghi bt u t ch s 1.
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.RecordStore;
/**
* @author Hand In Hand -- DTVTK7A- DH CNTT- TT Thai Nguyen
*/
public class login extends MIDlet implements CommandListener {
Display di;
Form fr;
TextField t1,t2;
Command c1,c2,c3;
RecordStore rs;
String st1,st2,st3,st4;
int i,j,k;
StringItem it;
public login()
{
//=============================================================
//
di = Display.getDisplay(this);
fr = new Form("ng nhp h thng !");
//=============================================================
//
t1 = new TextField("User","",50,TextField.ANY);
t2 = new TextField("Pass","",50,TextField.ANY);
fr.append(t1);
fr.append(t2);
//============================================================
=//
it = new StringItem("Chc mng :","");
fr.append(it);
//=============================================================
//
c1 = new Command("ng nhp",Command.CANCEL,1);
c2 = new Command("ng k",Command.BACK,2);
fr.addCommand(c1);
fr.addCommand(c2);
fr.setCommandListener(this);
//=============================================================
=//
}
public void startApp() {
di.setCurrent(fr);
}
public void pauseApp() {
}
public void destroyApp(boolean unconditional) {
}
//=============================================================
==//
public void openRestore(boolean b)
{
try{
rs = RecordStore.openRecordStore("data", b);
}
catch(Exception e){}
}
//============================================================
==//
public void closeRestore()
{
try {
rs.closeRecordStore();
}catch(Exception e) {}
}
//=============================================================
===//
public void commandAction(Command c, Displayable d) {
if(c==c2)
{
try {
openRestore(true);//mo de ghi
st1 = t1.getString();
st2 = t2.getString();
rs.addRecord(st1.getBytes(), 0, st1.length());// ghi du lieu byte
rs.addRecord(st2.getBytes(),0,st2.length());
closeRestore(); //dong
it.setText(" ghi d liu thnh cng !");
}
catch(Exception e){}
}
else
if(c==c1)
{
try{
openRestore(false);// mo de doc
int s = rs.getNumRecords();
for(i=1;i<s;i++)
{
st3 = new String(rs.getRecord(i));// doc chi so o day