以文本方式查看主题

-  课外天地 李树青  (http://njcie.com/bbs/index.asp)
--  JavaME移动开发课件  (http://njcie.com/bbs/list.asp?boardid=18)
----  程序代码——RMS对记录的常见操作  (http://njcie.com/bbs/dispbbs.asp?boardid=18&id=571)

--  作者:admin
--  发布时间:2008/10/21 21:15:22
--  程序代码——RMS对记录的常见操作

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编辑过]