itemdb.java

来自「Auction class for mobile development」· Java 代码 · 共 92 行

JAVA
92
字号
import javax.microedition.rms.*;
import java.util.Enumeration;
import java.util.Vector;
import java.io.*;

public class ItemDB {
  RecordStore recordStore = null;

  public ItemDB(String name) {
    // Open the record store using the specified name
    try {
      recordStore = open(name);
    }
    catch(RecordStoreException e) {
      e.printStackTrace();
    }
  }

  public RecordStore open(String fileName) throws RecordStoreException {
    return RecordStore.openRecordStore(fileName, true);
  }

  public void close() throws RecordStoreNotOpenException,
    RecordStoreException {
    // If the record store is empty, delete the file
    if (recordStore.getNumRecords() == 0) {
      String fileName = recordStore.getName();
      recordStore.closeRecordStore();
      recordStore.deleteRecordStore(fileName);
    }
    else {
      // Otherwise, close the record store
      recordStore.closeRecordStore();
    }
  }

  public Item getItemRecord(int id) {
    // Get the item record from the record store
    try {
      return (new Item(recordStore.getRecord(id)));
    }
    catch (RecordStoreException e) {
      e.printStackTrace();
    }

    return null;
  }

  public synchronized void setItemRecord(int id, String record) {
    // Convert the string record to an array of bytes
    byte[] bytes = record.getBytes();

    // Set the record in the record store
    try {
      recordStore.setRecord(id, bytes, 0, bytes.length);
    }
    catch (RecordStoreException e) {
      e.printStackTrace();
    }
  }

  public synchronized int addItemRecord(String record) {
    // Convert the string record to an array of bytes
    byte[] bytes = record.getBytes();

    // Add the byte array to the record store
    try {
      return recordStore.addRecord(bytes, 0, bytes.length);
    }
    catch (RecordStoreException e) {
      e.printStackTrace();
    }

    return -1;
  }

  public synchronized void deleteItemRecord(int id) {
    // Delete the item record from the record store
    try {
      recordStore.deleteRecord(id);
    }
    catch (RecordStoreException e) {
      e.printStackTrace();
    }
  }

  public synchronized RecordEnumeration enumerateItemRecords()
    throws RecordStoreNotOpenException {
    return recordStore.enumerateRecords(null, null, false);
  }
}

⌨️ 快捷键说明

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