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

📄 datarsio.java

📁 《神州》RPG游戏引擎
💻 JAVA
字号:
import javax.microedition.rms.*;

public class DataRsIO
{
    private RecordStore rs;
    private String storeName;

    public DataRsIO()
    {
    }

    public DataRsIO(String strName)
    {
        if (strName != "")
            storeName = strName;
    }


    public boolean open(String storeName)
    {
        try
        {
            rs = RecordStore.openRecordStore(storeName, true);  //createIfNecessary - if true, the record store will be created if necessary
            return true;
        }
        catch (RecordStoreException e)
        {
            e.printStackTrace();
            return false;
        }
    }

    public int insert(byte[] inBytes)
    {
        byte[] b = inBytes;
        int rsId = -1;

        try
        {
            rsId = rs.getNextRecordID();
            rs.addRecord(b, 0, b.length);
        }
        catch(RecordStoreException e)
        {
            e.printStackTrace();
        }
        return rsId;
    }

    public int insert(String str)
    {
        //System.out.println(str);
        if (str.equals(""))
            return -1;

        byte[] b = Tools.getUTFByte(str);
        int rsId = -1;

        try
        {
            rsId = rs.getNextRecordID();
            rs.addRecord(b, 0, b.length);
        }
        catch(RecordStoreException e)
        {
            e.printStackTrace();
        }

        return rsId;
    }

    public byte[] queryByRId(int rId)
    {
        byte[] recBytes = getRecordBytes(rId);

        return recBytes;
    }

    public String query(int rId)
    {
        byte[] recBytes = getRecordBytes(rId);
        String retString = Tools.getUTFStr(recBytes);

        return retString;
    }

    public void update(int rId, byte[] inBytes)
    {
        try
        {
            rs.setRecord(rId, inBytes, 0, inBytes.length);
        }
        catch(RecordStoreException e)
        {
           e.printStackTrace();
        }
    }

    public void update(int rId, String strInput)
    {

        byte[] inBytes = Tools.getUTFByte(strInput);

        try
        {
            rs.setRecord(rId, inBytes, 0, inBytes.length);
        }
        catch(RecordStoreException e)
        {
           e.printStackTrace();
        }
    }

    public void delete(int rId)
    {
        try {
            rs.deleteRecord(rId);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void delete(RecordFilter f, RecordComparator c)
    {
        try
        {
            RecordEnumeration enumRec = rs.enumerateRecords(f, c, false);

            while (enumRec.hasNextElement())
            {
                int rId = enumRec.nextRecordId();
                rs.deleteRecord(rId);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public RecordEnumeration getRecordEnum()
    {
        try
        {
            RecordEnumeration enumRec = rs.enumerateRecords(null, null, false);
            return enumRec;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    public RecordEnumeration getRecordEnum(RecordFilter f, RecordComparator c)
    {
        try
        {
            RecordEnumeration enumRec = rs.enumerateRecords(f, c, false);
            return enumRec;
        }
        catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

    public byte[] getRecordBytes(int rId)
    {
        try {
            byte[] recByte = rs.getRecord(rId);
            return recByte;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void clearAll()
    {
        try
        {
            RecordEnumeration enumRec = rs.enumerateRecords(null, null, false);

            while (enumRec.hasNextElement())
            {
                int rId = enumRec.nextRecordId();
                rs.deleteRecord(rId);
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public void close()
    {
        try {
            rs.closeRecordStore();
        }
        catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        }
        catch (RecordStoreException ex) {
            ex.printStackTrace();
        }
        rs = null;
    }

    public int getSize()
    {
        try {
            return rs.getSize();
        }
        catch (RecordStoreNotOpenException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public int getSizeAvailable()
    {
        try {
            return rs.getSizeAvailable();
        }
        catch (RecordStoreNotOpenException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public int getNumRecords()
    {
        try {
            return rs.getNumRecords();
        }
        catch (RecordStoreNotOpenException e) {
            e.printStackTrace();
            return -1;
        }
    }

    public String getStoreName()
    {
        return storeName;
    }

    public void setStoreName(String storeName)
    {
        this.storeName = storeName;
    }

    public RecordStore getRs()
    {
        if (rs!=null)
            return rs;
        else
            return null;
    }
}

⌨️ 快捷键说明

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