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

📄 calendarstore.java

📁 图书中的经典例子 好好看看 会有帮助的。java2me版本的
💻 JAVA
字号:
//CalendarStore.java
//实现记录文件的管理,记录的读写
package calendar;
import java.util.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

public class CalendarStore 
{
    public static final String recName="calendar"; //记录文件名称
    public RecordStore rec=null; //保存记录文件对象
    protected Alert alert; //用于显示异常时的错误信息
    protected Display display;
    //构造器
    public CalendarStore(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());
    }
    //检测记录文件是否存在
    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);
            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.setMode(RecordStore.AUTHMODE_ANY, false);
            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;
    }
    //打开所有包含所有记录的结果集
    public RecordEnumeration openAllRecords(boolean autoUpdate)
    {
        RecordEnumeration enum = null;
        try
        {
            enum = rec.enumerateRecords(null, null, autoUpdate);
        }
        catch (Exception e) { ShowError(e); }
        return enum;
    }
    //增加记录
    public boolean addRecord(byte[] src,int offset, int numBytes)
    {
        try
        {
            int id = rec.addRecord(src, offset, numBytes);
            return true;
        }
        catch (Exception e) { ShowError(e); }
        return false;
    }
    //通过记录号得到记录内容
    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 + -