J2ME数据库源码

以下代码是J2ME简单的数据库源码,提供给大家分享

package cn.hipha.util;

import javax.microedition.rms.RecordStore;

public class JmeDB {

 private RecordStore rs = null;

 public JmeDB() {
 }

 public JmeDB(String dbName) {
  openDB(dbName);
 }

 public void openDB(String dbName) {
  try {
   rs = RecordStore.openRecordStore(dbName, true);
  } catch (Exception e) {
  }
 }

 public String getRecord(String name) throws Exception {
  byte[] bytes = getRecord(name, 1);
  if (bytes != null && bytes.length > 0)
   return new String(bytes);
  return "";
 }

 public byte[] getRecord(String name, int index) throws Exception {
  RecordStore rss = getRecordSotre(name);
  byte[] bytes = getRecord(rss, index);
  closeRecordStore(rss);
  return bytes;
 }

 private byte[] getRecord(RecordStore rs, int index) throws Exception {
  if (rs == null) {
   throw new Exception("Database desn't open!");
  }
  try {
   if (rs.getNumRecords() >= index) {
    byte[] byte1 = rs.getRecord(index);
    return byte1;
   }
  } catch (Exception e) {
  }
  return new byte[0];
 }

 public void setRecord(String name, String record) throws Exception {
  setRecord(name, 1, record);
 }

 public void setRecord(String name, int index, String record)
   throws Exception {
  byte[] data = record.getBytes("UTF-8");
  setRecord(name, index, data);
 }

 public void setRecord(String name, byte[] data)throws Exception {
  setRecord(name, 1, data);
 }
 
 public void setRecord(String name, int index, byte[] data)throws Exception {
  RecordStore rss = getRecordSotre(name);
  setRecord(rss, index, data);
  closeRecordStore(rss);
 }
 
 protected boolean hasRecord(RecordStore rs, String name) throws Exception {
  if (rs == null) {
   throw new Exception("Database desn't open!");
  }
  boolean flag = false;
  for (int i = 1; i <= rs.getNumRecords(); i++) {
   byte[] bytes = getRecord(rs, i);
   if (bytes != null && bytes.length > 0) {
    String temp = new String(bytes);
    if (name.trim().equals(temp.trim())) {
     flag = true;
     break;
    }
   }
  }
  return flag;
 }

 private RecordStore getRecordSotre(String name) throws Exception {
  if (hasRecord(rs, name)) {
   return RecordStore.openRecordStore(name, false);
  } else {
   byte[] data = name.getBytes("UTF-8");
   setRecord(rs, rs.getNextRecordID(), data);
   return RecordStore.openRecordStore(name, true);
  }
 }

 private void setRecord(RecordStore rs, int index, byte[] data)
   throws Exception {
  if (rs == null) {
   throw new Exception("Database desn't open!");
  }
  try {
   if (data != null) {
    int id = rs.getNextRecordID();
    if (id <= index) {
     rs.addRecord(data, 0, data.length);
    } else {
     rs.setRecord(index, data, 0, data.length);
    }
   }
  } catch (Exception e) {
  }
 }

 public void closeDB() {
  closeRecordStore(rs);
 }
 
 private void closeRecordStore(RecordStore rs){
  if (rs != null) {
   try {
    rs.closeRecordStore();
   } catch (Exception e) {
   }
  }
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值