⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbdemo2.java

📁 J2ME MIDP 2.0 无线设备编程的一些源码
💻 JAVA
字号:
//dbDemo2.java file
import javax.microedition.lcdui.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;

public class dbDemo2 extends MIDlet implements CommandListener
{
    //命令对象,退出;增加;向后遍历;向前遍历;删除
    private Command exitCommand, addCmd, enumCmd, enumCmd2, delCmd ;
    private TextBox tb; //通过TextBox来显示信息
    private UsersRecordStore userRec; //数据操作对象
    private java.util.Random rnd = new java.util.Random();
    public dbDemo2()
    {
        tb =new TextBox("OutputWindow","",120,TextField.ANY);
        exitCommand =new Command("Exit",Command.EXIT,1);
        addCmd =new Command("addCmd",Command.SCREEN,1);
        enumCmd =new Command("enumNext",Command.SCREEN,1);
        enumCmd2 =new Command("enumPrev",Command.SCREEN,1);
        delCmd =new Command("delCmd",Command.SCREEN,1);
        tb.addCommand(exitCommand);
        tb.addCommand(addCmd);
        tb.addCommand(enumCmd);
        tb.addCommand(enumCmd2);
        tb.addCommand(delCmd);
        tb.setCommandListener(this);
        userRec = new UsersRecordStore(Display.getDisplay(this));
        openDb(); //程序启动时候打开记录文件
    }
    protected void openDb( )
    {
        //如果记录文件不存在则创建记录文件
        if(! userRec.IsDbExist())
            userRec.CreateDb();
        userRec.OpenDb();//打开记录文件
    }
    protected void startApp(  ) throws MIDletStateChangeException
    {
        Display.getDisplay(this).setCurrent(tb);
    }

    protected void pauseApp(  )
    {
    }

    protected void destroyApp( boolean p1 )
    {
        userRec.CloseDb();
    }

    public void commandAction(Command c,Displayable d)
    {
        if (c ==exitCommand)
        {
            destroyApp(false);
            notifyDestroyed();
        }
        else if(c ==addCmd)
        {//添加一个随机的用户信息作为测试
            String rname= "name"+rnd.nextInt();
            String remail = rname+"@vchelp.net";
            int recId = userRec.AddRecord(rname,"F","N/A",remail);
            tb.setString("RecordId = "+ recId + " added");
        }
        else if(c ==enumCmd)
        {
            RecordEnumeration enu = userRec.OpenAllRecord(false);
            tb.setString("recordNum = " + enu.numRecords());
            RecordData rData = new RecordData();
            while( enu.hasNextElement() )
            {
                try
                {
                    rData.recordId = enu.nextRecordId();//得到下一条记录号
                    rData.ReadByte(userRec.getRecord(rData.recordId)); //得到记录内容并分解
                    rData.Trace();//输出记录信息
                }
                catch(Exception e){}
            }
            enu.destroy();
            enu = null;
            rData = null;
        }
        else if(c ==enumCmd2)
        {
            RecordEnumeration enu = userRec.OpenAllRecord(false);
            tb.setString("recordNum = " + enu.numRecords());
            RecordData rData = new RecordData();
            while( enu.hasPreviousElement() )
            {
                try
                {
                    rData.recordId = enu.previousRecordId();//得到下一条记录号
                    rData.ReadByte(userRec.getRecord(rData.recordId)); //得到记录内容并分解
                    rData.Trace();//跟踪输出记录信息
                }
                catch(Exception e){}
            }
            enu.destroy();
            enu = null;
            rData = null;
        }
        else if(c ==delCmd)
        {
            RecordEnumeration enu = userRec.OpenAllRecord(false);
            tb.setString("recordNum = " + enu.numRecords());
            while( enu.hasPreviousElement() )
            {
                try
                {
                    int recId = enu.previousRecordId();//得到下一条记录号
                    userRec.DelRecord(recId);
                }
                catch(Exception e){}
            }
            enu.destroy();
            enu = null;
            tb.setString(tb.getString() + " delete all records");
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -