当前位置:编程学习 > JAVA >>

JavaMe连载(7)-数据永久存储

【问题描述】JavaMe不支持关系型数据库的操作,如何实现永久存储呢?

JavaMe中,之所以没有不支持数据库,是因为功能性手机一般存储空间较小。所以采用一种成为“记录”的概念代替,即采用一种成为RMS的机制进行永久存储。

 

【实例】

UserDataItem.java

[html] package com.token.model; 
 
import com.token.util.StringDealMethod; 
 
public class UserDataItem { 
    private int id; 
    public String name = null; 
    public String passwd = null; 
 
    public UserDataItem(String name,String passwd) 
    { 
        this.name = name; 
        this.passwd = passwd;    
    } 
     
    public UserDataItem(int id,byte[] data){ 
        this.id=id; 
        String temp=new String(data); 
        String temp_sub[] = StringDealMethod.split(temp, ","); 
        this.name = temp_sub[0]; 
        this.passwd = temp_sub[1]; 
    } 
     
    public int getId(){ 
        return id; 
    } 
     
    public void setId(int id){ 
        this.id=id; 
    } 
     
    public String getName(){ 
        return name; 
    } 
     
    public void setName(String name){ 
        this.name = name; 
    } 
     
    public String getPasswd(){ 
        return passwd; 
    } 
     
    public void setPasswd(String passwd){ 
        this.passwd = passwd; 
    } 
     
    public byte[] getBytes(){ 
        String temp=null; 
         
        if(name==null||passwd==null){ 
            return null; 
        }else{ 
            temp=name+","+passwd; 
        } 
         
        return temp.getBytes(); 
    } 

package com.token.model;

import com.token.util.StringDealMethod;

public class UserDataItem {
 private int id;
    public String name = null;
    public String passwd = null;

    public UserDataItem(String name,String passwd)
    {
     this.name = name;
     this.passwd = passwd; 
    }
   
    public UserDataItem(int id,byte[] data){
        this.id=id;
     String temp=new String(data);
     String temp_sub[] = StringDealMethod.split(temp, ",");
     this.name = temp_sub[0];
     this.passwd = temp_sub[1];
    }
   
    public int getId(){
     return id;
    }
   
    public void setId(int id){
     this.id=id;
    }
   
    public String getName(){
        return name;
    }
   
    public void setName(String name){
        this.name = name;
    }
   
    public String getPasswd(){
        return passwd;
    }
   
    public void setPasswd(String passwd){
        this.passwd = passwd;
    }
   
    public byte[] getBytes(){
        String temp=null;
       
        if(name==null||passwd==null){
            return null;
        }else{
            temp=name+","+passwd;
        }
       
        return temp.getBytes();
    }
}
 

UserDataRecord.java

[html] package com.token.util; 
 
import java.util.Vector; 
 
import javax.microedition.rms.RecordComparator; 
import javax.microedition.rms.RecordEnumeration; 
import javax.microedition.rms.RecordStore; 
import javax.microedition.rms.RecordStoreException; 
 
import com.token.model.*; 
 
public class UserDataRecord { 
    private static final String RECORDSTORE_NAME="USER_DB"; 
    private static RecordStore info; 
     
    public UserDataRecord(){ 
    } 
     
    //打开RecordStore,没有则创建 
    public void openDataBase() { 
        try { 
            info = RecordStore.openRecordStore(RECORDSTORE_NAME, true); 
        }catch (RecordStoreException ex) {             
            info =null; 
        } 
    } 
    //关闭RecordStore 
    public void closeDataBase() { 
        if (info!= null) { 
            try { 
           &nbs

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,