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

📄 db_rs.java

📁 一个J2ME Twitter 客户端的NetBeans项目
💻 JAVA
字号:
/*
 ******************************************************************************
    Implements a simple database using recordstore
    Copyright (C) 2007 Timothy Lim Sheng Hwee

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License along
    with this program; if not, write to the Free Software Foundation, Inc.,
    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.

 Author: Timothy Lim Sheng Hwee
 Email : LimShengHwee@gmail.com
******************************************************************************
 
 
 stored in RMS
    can be scalar value     (1 entry for each variable name)
        write(var, val, append == false);
    or vector/array value   (several entries with same variable name)
        write(var, val, append == false);     // val replaces existing value
        write(var, val, append == true);      // val added to existing
            need to manually handle vector
 
 */

package Twittering;

import javax.microedition.rms.*;
import java.util.Hashtable;
import java.util.Vector;
import java.lang.Exception.*;
/**
 *
 * @author Administrator
 */
public class DB_RS
{
    private String name;
    private RecordStore rs;
    private Exception error;
    private Hashtable variables;
    
    class DB_RS_Entry
    {
        int id;
        String variable;
        String value;
        public DB_RS_Entry()
        {
            id = 0;
            variable = null;
            value = null;
        }
        public DB_RS_Entry(int id, String var, String val)
        {
            this.id = id;
            variable = var;
            value = val;
        }
        public int getId() { return id; }
        public void setId(int i) { id = i; }
        public String getVariable(){ return variable; }
        public void setVariable(String v){ variable = v; }
        public String getValue(){ return value; }
        public void setValue(String v){ value = v; }
        public String toString()
        {
            return value;
        }
            
    }
    
    public DB_RS(String name) 
    {
        this.name = name;
        error = null;
        variables = new Hashtable();
    }
    public String getName(){ return name; }
    public void open() throws Exception
    {
        log("Open DB");
        rs = RecordStore.openRecordStore(name, true);
        log("DEBUG: num rec: " + rs.getNumRecords());
    }
    public void close() throws Exception 
    {
        log("Close DB");
        rs.closeRecordStore();
    }
    public void delete() throws Exception
    {
        log("Delete DB");
        RecordStore.deleteRecordStore(name);
    }
    
    public void initialise()  throws Exception
    {
            log("Initialise DB");
            byte[] recData;
            int len, pos;
            String tmp, val, var;
            
            RecordEnumeration entries = rs.enumerateRecords(null, null, false);
            
            //for (int i = 1; i <= rs.getNumRecords(); i++) 
            while(entries.hasNextElement())
            {
                int recID = entries.nextRecordId();
                recData = new byte[rs.getRecordSize(recID)];
                len = rs.getRecord(recID, recData, 0);
                tmp = new String(recData, 0, len);
                switch((pos = tmp.indexOf('|')))
                {
                    case -1:
                    {
                        break;
                    }
                    default:
                    {
                        var = tmp.substring(0, pos);
                        val = null;
                        if (pos < tmp.length()) val = tmp.substring(pos+1);
                        Vector v = (Vector) variables.get(var);
                        DB_RS_Entry entry = new DB_RS_Entry(recID, var, val);
                        if (v != null)
                        {
                            v.addElement(entry);
                        }
                        else
                        {
                            v = new Vector();
                            v.insertElementAt(entry,Functions.insertAlphaGetPos(v, entry.toString(), 0, v.size()-1, true));
                            variables.put(var, v);
                        }
                        break;
                    }                    
                }
            }
    }    
    public void delete(String var) throws Exception
    {
        Vector v = (Vector) variables.remove(var);
        if (v != null)
        {                
            for(int i = 0; i < v.size(); i++)
               rs.deleteRecord(((DB_RS_Entry) v.elementAt(i)).getId());
        }
    }    
    public void write(String var, String val, boolean append) throws Exception
    {
        System.out.print("DB Write: ");
        if (append) System.out.print("append ");
        System.out.println(var + "=" + val);
            Vector v = (Vector) variables.get(var);
            
            if ((v != null) && !append)
            {                
                for(int i = 0; i < v.size(); i++)
                   rs.deleteRecord(((DB_RS_Entry) v.elementAt(i)).getId());
            }
            byte[] bytes = (new String(var + "|" + val)).getBytes();
            DB_RS_Entry newEntry = new DB_RS_Entry(rs.addRecord(bytes, 0, bytes.length), var, val);

            // add to existing if any
            // if exists: add to vector
            // else create new vector
            if (append)
            {
                if (v == null) v = new Vector();
                v.addElement(newEntry);
                variables.put(var, v);
            }
            else
            {
                if (v != null) variables.remove(v);
                v = new Vector(); 
                v.addElement(newEntry);
                variables.put(var, v);
            }
    }
    public Vector getValueVector(String var)
    {
        return (Vector) variables.get(var);
    }
    public String getValueScalar(String var)
    {
        Vector v = (Vector) variables.get(var);
        String str;
        if ((v == null) || (v.size() == 0))
            str = null;
        else
            str = getRowValue(var, 0);
        return str;
    }
    public String getRowValue(String var, int i)
    {
        Vector v = (Vector) variables.get(var);
        if (v == null) return null;
        return (String) ((DB_RS_Entry) v.elementAt(i)).getValue();
    }
    
    public void log(String str)
    {
        System.out.println(str);
    }
    public Exception getError() { return error; }
}

⌨️ 快捷键说明

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