J2me 入门路之---3 RecordStore

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[size];
}
rs.getRecord(i, data, 0);
rec += "\n" + new String(data,0,size);
}
catch (RecordStoreException e) {
}
}
}
return rec;
}
// 打开(创建)记录存储
}
[align=left][/align]
$(function(){ $.fn.extend({ SimpleTree:function(options){ //初始化参数 var option = $.extend({ click:function(a){ } },options); option.tree=this; /* 在参数对象中添加对当前菜单树的引用,以便在对象中使用该菜单树 */ option._init=function(){ /* * 初始化菜单展开状态,以及分叉节点的样式 */ this.tree.find("ul ul").hide(); /* 隐藏所有子级菜单 */ this.tree.find("ul ul").prev("li").removeClass("open"); /* 移除所有子级菜单父节点的 open 样式 */ this.tree.find("ul ul[show='true']").show(); /* 显示 show 属性为 true 的子级菜单 */ this.tree.find("ul ul[show='true']").prev("li").addClass("open"); /* 添加 show 属性为 true 的子级菜单父节点的 open 样式 */ }/* option._init() End */ /* 设置所有超链接不响应单击事件 */ this.find("a").click(function(){ $(this).parent("li").click(); return false; }); /* 菜单项 接受单击 */ this.find("li").click(function(){ /* * 当单击菜单项 * 1.触发用户自定义的单击事件,将该 标签中的第一个超链接做为参数传递过去 * 2.修改当前菜单项所属的子菜单的显示状态(如果等于 true 将其设置为 false,否则将其设置为 true) * 3.重新初始化菜单 */ option.click($(this).find("a")[0]); /* 触发单击 */ /* * 如果当前节点下面包含子菜单,并且其 show 属性的值为 true,则修改其 show 属性为 false * 否则修改其 show 属性为 true */ /* if($(this).next("ul").attr("show")=="true"){ $(this).next("ul").attr("show","false"); }else{ $(this).next("ul").attr("show","true"); }*/ /* 初始化菜单 */ option._init(); }); /* 设置所有父节点样式 */ this.find("ul").prev("li").addClass("folder"); /* 设置节点“是否包含子节点”属性 */ this.find("li").find("a").attr("hasChild",false); this.find("ul").prev("li").find("a").attr("hasChild",true); /* 初始化菜单 */ option._init(); }/* SimpleTree Function End */ }); });
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值