ITeye博客

J2me 入门路之---3 RecordStore

yzhong_sa · 2009-02-11 19:06 · C · C++ · C# · Markdown

package com.sa.demo.rs;

import javax.microedition.lcdui.Command; import javax.microedition.lcdui.CommandListener; import javax.microedition.lcdui.Display; import javax.microedition.lcdui.Displayable; import javax.microedition.lcdui.TextBox; import javax.microedition.lcdui.TextField; import javax.microedition.midlet.MIDlet; import javax.microedition.midlet.MIDletStateChangeException; import javax.microedition.rms.RecordStore; import javax.microedition.rms.RecordStoreException; import javax.microedition.rms.RecordStoreFullException; import javax.microedition.rms.RecordStoreNotFoundException; import javax.microedition.rms.RecordStoreNotOpenException;

public class TestRS2 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 TestRS2() {

	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打开失败";
}

private boolean OpenRS(String rsName) {
	// TODO Auto-generated method stub
	try {
		rs = RecordStore.openRecordStore("rsName", true);
		return true;
	} catch (RecordStoreFullException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return false;
	} catch (RecordStoreNotFoundException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return false;
	} catch (RecordStoreException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
		return false;
	}
	
}

protected void destroyApp(boolean arg0) throws MIDletStateChangeException {
	// TODO Auto-generated method stub
	if (CloseRS())
		System.out.println("记录存储myRS关闭成功");
	else
		System.out.println("记录存储myRS关闭失败");
}

private boolean CloseRS() {
	// TODO Auto-generated method stub
	return false;
}

protected void pauseApp() {
	// TODO Auto-generated method stub

}

protected void startApp() throws MIDletStateChangeException {
	// TODO Auto-generated method stub
	showMsg.setString(msg);
	display.setCurrent(showMsg);
}

public void commandAction(Command c,Displayable d) {
	int recId;
	if (c == exitCommand) {
		try {
			destroyApp(false);
		} catch (MIDletStateChangeException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		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);
	}
}

 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;
    }

private int AddRec() {
	// TODO Auto-generated method stub
	int id = -1;
	int recID = -1;
	try {
		id = rs.getNextRecordID();//添加记录 返回记录的位置
		
	} catch (RecordStoreNotOpenException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (RecordStoreException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	String data = "Data of Record :"+id;
	byte rec = data.getBytes();
	try {
		recID = rs.addRecord(rec, 0, rec.length);//插入数据。
		
	} catch (RecordStoreNotOpenException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (RecordStoreFullException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	} catch (RecordStoreException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return recID ;
}
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;
					}
					rs.getRecord(i, data, 0);
					rec += "\n" + new String(data,0,size);
				}
				catch (RecordStoreException e) {
				}
			}
		}
        return rec;
    }
	// 打开(创建)记录存储

}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。