📄 usersrecordstore.java
字号:
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
public class UsersRecordStore
{
public final String recName="chap8db"; //记录文件名称
public RecordStore rec=null; //保存记录文件对象
protected Alert alert; //用于显示异常时的错误信息
protected Display display;
public UsersRecordStore(Display dis)
{
display = dis;
alert = new Alert("RMS error !");
alert.setTimeout(5000);
}
//显示错误信息
protected void ShowError(Exception e)
{//通过警告窗口显示错误信息
alert.setString(e.toString());
display.setCurrent(alert, display.getCurrent());
}
//v0.1 功能 记录文件的 打开,关闭,创建,删除
//检测记录文件是否存在
public boolean IsDbExist()
{
try
{//打开然后关闭记录文件,通过异常类型来判断记录文件是否存在
RecordStore rs = RecordStore.openRecordStore(recName, false);
rs.closeRecordStore();
rs=null;
}
catch(RecordStoreFullException e1)
{
return true;
}
catch(RecordStoreNotFoundException e2)
{//捕捉到记录不存在的错误,表示记录文件不存在
return false;
}
catch(RecordStoreException e3)
{
ShowError(e3);
}
return true;
}
//打开记录文件
public boolean OpenDb()
{
try
{
rec = RecordStore.openRecordStore(recName, false);
//打开文件后检测文件的最后修改时间,在正式版本中可以删除
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(rec.getLastModified()));
System.out.println("LastModified = " + cal.toString() );
//检查空间
System.out.println("getSize = "+ rec.getSize());
System.out.println("getSizeAvailable = " + rec.getSizeAvailable() );
return true;
}
catch(Exception e)
{
ShowError(e);
}
return false;
}
//关闭记录文件
public boolean CloseDb()
{
if(rec != null)
{
try
{
rec.closeRecordStore();
rec = null;
return true;
}
catch(Exception e)
{
ShowError(e);
}
}
return false;
}
//创建记录文件
public boolean CreateDb()
{//创建文件前请先检查记录文件是否已经存在
try
{//通过打开记录文件的方式来进行创建
RecordStore rs = RecordStore.openRecordStore(recName, true);
rs.closeRecordStore();
rs=null;
return true;
}
catch(Exception e)
{
ShowError(e);
}
return false;
}
//删除记录文件
public boolean DeleteDb()
{//删除文件前请先检查记录文件是否已经存在
try
{
RecordStore.deleteRecordStore(recName);
return true;
}
catch(Exception e)
{
ShowError(e);
}
return false;
}
//v0.2功能 记录的添加,删除,更新,创建结果集合
//增加记录
public int AddRecord(String name,String sex,String birth,String email)
{//返回被增加的记录号,返回-1 表示出错
String buffer = name + "&" + sex + "&" + birth + "&" + email;
byte[ ] data = buffer.getBytes();
int recId = -1;
try
{
recId = rec.addRecord(data,0, data.length);
}
catch (Exception e)
{
ShowError(e);
}
return recId ;
}
//删除记录
public boolean DelRecord(int recId)
{
try
{
rec.deleteRecord(recId);
}
catch (Exception e)
{
ShowError(e);
return false;
}
return true;
}
//更新记录
public boolean UpdateRecord(int recId,String name,String sex,String birth,String email)
{
String buffer = name + "&" + sex + "&" + birth + "&" + email;
byte[ ] data = buffer.getBytes();
try
{
rec.setRecord(recId, data,0, data.length);
}
catch (Exception e)
{
ShowError(e);
return false;
}
return true ;
}
//打开所有包含所有记录的结果集
public RecordEnumeration OpenAllRecord(boolean autoUpdate)
{
RecordEnumeration enum = null;
try
{
enum = rec.enumerateRecords(null, null, autoUpdate);
}
catch (Exception e)
{
ShowError(e);
}
return enum;
}
//通过记录号得到记录内容
public byte[] getRecord(int recId)
throws RecordStoreNotOpenException, InvalidRecordIDException, RecordStoreException
{
return rec.getRecord(recId);
}
//v0.3功能 查找记录,对记录进行排序
//查找指定姓名的用户
public RecordEnumeration FindRecord(String name)
{
RecordEnumeration enum = null;
try
{
UserFindFilter filter = new UserFindFilter(UserFindFilter.FULLMATCH,name);
enum = rec.enumerateRecords(filter, null, false);
}
catch (Exception e)
{
ShowError(e);
}
return enum;
}
//创建按照姓名排序的结果集
public RecordEnumeration SortRecordByName()
{
RecordEnumeration enum = null;
try
{
UserSorter sorter = new UserSorter(UserSorter.SORTBYNAME);
enum = rec.enumerateRecords(null, sorter, false);
}
catch (Exception e)
{
ShowError(e);
}
return enum;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -