课外天地 李树青学习天地JavaME移动开发课件 → 程序代码——RMS对记录的常见操作


  共有15039人关注过本帖树形打印复制链接

主题:程序代码——RMS对记录的常见操作

帅哥哟,离线,有人找我吗?
admin
  1楼 博客 | 信息 | 搜索 | 邮箱 | 主页 | UC


加好友 发短信 管理员
等级:管理员 帖子:1945 积分:26736 威望:0 精华:34 注册:2003/12/30 16:34:32
程序代码——RMS对记录的常见操作  发帖心情 Post By:2008/10/21 21:15:22 [只看该作者]

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.util.Calendar;
import java.util.Date;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordStore;

public class Exec extends MIDlet implements CommandListener {
        private Display display;
        Form form = new Form("练习");
        RecordStore rs;
        TextField tf;
        Command com1 = new Command("查看属性", Command.BACK, 1);
        Command com2 = new Command("增加记录", Command.OK, 1);
        Command com3 = new Command("获取记录", Command.OK, 2);
        Command com4 = new Command("更新记录", Command.OK, 3);
        Command com5 = new Command("删除记录", Command.OK, 4);

        public Exec() {
                display = Display.getDisplay(this);
        }

        public void startApp() {
                form.addCommand(com1);
                form.addCommand(com2);
                form.addCommand(com3);
                form.addCommand(com4);
                form.addCommand(com5);
                form.setCommandListener(this);
                display.setCurrent(form);
        }

        public void pauseApp() {
        }

        public void destroyApp(boolean unconditional) {
        }

        public void commandAction(Command com, Displayable disp) {
                form.deleteAll();
                if (com == com1) {
                        try {
                                rs = RecordStore.openRecordStore("Test1", true);// 名称不能重复,true表示创建之
                                if (rs.getLastModified() != 0) {
                                        Date date = new Date(rs.getLastModified());
                                        tf = new TextField("记录集创建时间:", date.toString(), 40,
                                                        TextField.ANY);
                                        form.append(tf);
                                }
                                tf = new TextField("记录集名称:", String.valueOf(rs.getName()), 20,
                                                TextField.ANY);
                                form.append(tf);
                                tf = new TextField("记录数目:", String.valueOf(rs.getNumRecords()),
                                                20, TextField.ANY);
                                form.append(tf);
                                tf = new TextField("记录集大小:", String.valueOf(rs.getSize()), 20,
                                                TextField.ANY);
                                form.append(tf);
                                tf = new TextField("存储空间剩余:", String.valueOf(rs
                                                .getSizeAvailable()), 20, TextField.ANY);
                                form.append(tf);
                                tf = new TextField("记录集版本:", String.valueOf(rs.getVersion()),
                                                20, TextField.ANY);
                                form.append(tf);
                        } catch (Exception e) {
                                System.out.println(e.getMessage());
                        }
                } else if (com == com2) {
                        try {
                                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                                DataOutputStream dos = new DataOutputStream(baos);
                                Calendar rightNow = Calendar.getInstance();
                                dos.writeUTF(rightNow.getTime().toString());
                                dos.close();
                                baos.close();
                                rs.addRecord(baos.toByteArray(), 0, baos.toByteArray().length);
                        } catch (Exception e) {
                                System.out.println(e.getMessage());
                        }
                } else if (com == com3) {
                        try {
                                int recordid = rs.getNextRecordID() - 1;
                                byte[] bytearray = new byte[rs.getRecordSize(recordid)];
                                rs.getRecord(recordid, bytearray, 0);
                                String record = new String(bytearray);
                                tf = new TextField("记录" + recordid + "创建时间:", record, 50,
                                                TextField.ANY);
                                form.append(tf);
                        } catch (Exception e) {
                                System.out.println(e.getMessage());
                        }
                } else if (com == com4) {
                        try {
                                int recordid = rs.getNextRecordID() - 1;
                                Calendar rightNow = Calendar.getInstance();
                                String date = rightNow.getTime().toString();
                                byte[] bytearray = date.getBytes();
                                rs.setRecord(recordid, bytearray, 0, bytearray.length);
                        } catch (Exception e) {
                                System.out.println(e.getMessage());
                        }
                } else if (com == com5) {
                        try {
                                rs.deleteRecord(1);
                        } catch (Exception e) {
                                System.out.println(e.getMessage());
                        }
                }
        }
}

[此贴子已经被作者于2010-12-12 18:38:11编辑过]

 回到顶部