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

📄 simplerecord.java

📁 很齐全通讯录
💻 JAVA
字号:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class SimpleRecord extends MIDlet implements CommandListener{
	private Command exitCommand, addCmd, delCmd, setCmd, getCmd;
	private Display display;
	private RecordStore rs;		// 定义记录存储rs
	private TextBox showMsg;	// 用于显示操作结果信息
	private String msg = "";	// 存放用于文本框显示的信息
	public SimpleRecord() {
		display = Display.getDisplay(this);
		showMsg = new TextBox("操作信息:", null, 512, TextField.ANY);
		exitCommand =new Command("退出",Command.EXIT,1);
		addCmd =new Command("添加记录",Command.SCREEN,1);
		delCmd =new Command("删除记录",Command.SCREEN,1);
		setCmd =new Command("修改记录",Command.SCREEN,1);
		getCmd =new Command("读取记录",Command.SCREEN,1);
		showMsg.addCommand(exitCommand);
		showMsg.addCommand(addCmd);
		showMsg.addCommand(delCmd);
		showMsg.addCommand(setCmd);
		showMsg.addCommand(getCmd);
		showMsg.setCommandListener(this);
		if (OpenRS("myRS"))
			msg += "记录存储myRS打开成功";
		else
			msg += "记录存储myRS打开失败";
	}
	public void startApp(){
		showMsg.setString(msg);
 		display.setCurrent(showMsg);
   }
	public void pauseApp() { }
	public void destroyApp(boolean unconditional) {
		if (CloseRS())
			System.out.println("记录存储myRS关闭成功");
		else
			System.out.println("记录存储myRS关闭失败");
	}
	public void commandAction(Command c,Displayable d) {
		int recId;
		if (c == exitCommand) {
			destroyApp(false);
			notifyDestroyed();
		}
		else if (c == addCmd) {
			if ((recId = AddRec()) != -1)
				msg = "记录添加成功,记录号为 " + recId;
			else
				msg = "记录添加失败";
			showMsg.setString(msg);
		}
		else if (c == delCmd) {
			if ((recId = DelRec()) != -1)
				msg = "记录删除成功,记录号为 " + recId;
			else
				msg = "记录删除失败";
			showMsg.setString(msg);
		}
		else if (c == setCmd) {
			if ((recId = SetRec()) != -1)
				msg = "记录修改成功,记录号为 " + recId;
			else
				msg = "记录修改失败";
			showMsg.setString(msg);
		}
		else if (c == getCmd) {
			msg = GetRec();
			showMsg.setString(msg);
		}
	}
    // 增加记录,返回所增加记录的记录号,返回-1表示出错
    public int AddRec() {
		int id;
        int recID = -1;
		try {
			id = rs.getNextRecordID();
		}
		catch (RecordStoreException e) {
	        return recID ;		
		}
        String rec = "Data of Record " + id;
        byte [] data = rec.getBytes();
        try {
            recID = rs.addRecord(data, 0, data.length);
        }
        catch (RecordStoreException e) { }
        return recID ;
    }
    // 删除最后一条记录,返回所删除记录的记录号,返回-1表示出错
    public int DelRec() {
		int id, num;
		try {
			id = rs.getNextRecordID();
			num = rs.getNumRecords();
		}
		catch (RecordStoreException e) {
	        return -1;		
		}
		if (num >0) {
			for (int i=id-1; i>0; i--) {
				try	{
					rs.deleteRecord(i);
					return i;
				}
				catch (RecordStoreException e) { }
			}
		}
        return -1;
    }
    // 修改最后一条记录,返回所修改记录的记录号,返回-1表示出错
    public int SetRec() {
		int id, num;
		try {
			id = rs.getNextRecordID();
			num = rs.getNumRecords();
		}
		catch (Exception e) {
	        return -1;		
		}
        String rec;
		byte [] data;
		if (num >0) {
			for (int i=id-1; i>0; i--) {
				 rec = "Modified Data of Record " + i;
				 data = rec.getBytes();
				 try {
					rs.setRecord(i, data, 0, data.length);
					return i;
				}
				catch (RecordStoreException e) { }
			}
		}
        return -1;
    }
    // 显示所有记录
    public String GetRec() {
		int id, num;
		try {
			id = rs.getNextRecordID();
			num = rs.getNumRecords();
		}
		catch (RecordStoreException e) {
	        return null;		
		}
        String rec = "记录存储中共有" + num + "条记录:";
		byte [] data = null;
		if (num >0) {
			for (int i=1; i<id; i++) {
				try {
					int size = rs.getRecordSize(i);
					if (data == null || data.length < size) {
						data = new byte[size];
					}
					rs.getRecord(i, data, 0);
					rec += "\n" + new String(data,0,size);
				}
				catch (RecordStoreException e) {
				}
			}
		}
        return rec;
    }
	// 打开(创建)记录存储
	public boolean OpenRS(String rsName) {
		try {
			rs = RecordStore.openRecordStore(rsName, true);
			return true;
		}
		catch(RecordStoreException e) { }
		return false;
	}
	// 关闭记录存储
	public boolean CloseRS() {
		if(rs != null) {
			try {
				rs.closeRecordStore();
				rs = null;
				return true;
			}
			catch(RecordStoreException e) { }
		}
		return false;
	}
}

⌨️ 快捷键说明

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