3
A
回答
8
這裏是我的庫代碼爲RMS,只是研究它,它是非常容易實現的,都喜歡插入,更新,刪除的方法是存在的。
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotOpenException;
import com.project.gui.components.CustomAlert;
import com.project.gui.midlet.MyMidlet;
public class RMSStore
{
private RecordStore rs = null;
public void openRecordStore(String str)
{
try
{
if(rs == null)
{
rs = RecordStore.openRecordStore(str, true);
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void closeRecordStore()
{
try
{
if(rs!=null)
{
rs.closeRecordStore();
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void deleteRecordStore(String storenName)
{
try
{
RecordStore.deleteRecordStore(storenName);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public void deleteRMS(String storenName)
{
int count = 0;
try
{
RecordStore newRS = RecordStore.openRecordStore(storenName, true);
count = newRS.getNumRecords();
newRS.closeRecordStore();
}
catch (Exception e)
{
System.out.println ("Error while Opening " + e.toString());
}
if (count > 0)
{
try
{
RecordStore.deleteRecordStore(storenName);
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
public static String[] listAllRecordStore()
{
return RecordStore.listRecordStores();
}
public boolean SearchRecord(String Rec)
{
String [] data = getRecordData();
for (int i = 0 ; i < data.length ; i++)
{
if (Rec.toString().trim().equals(data[i].toString().trim()))
{
data = null; // System.gc();
return true;
}
}
data = null; // System.gc();
return false;
}
public boolean SearchRecord(String Rec, int pos)
{
String [] data = getRecordData();
Rec = Rec.substring(0,pos);
for (int i = 0 ; i < data.length ; i++)
{
data[i] = data[i].substring(0, pos);
if (Rec.toString().trim().equals(data[i].toString().trim()))
{
data = null; // System.gc();
return true;
}
}
data = null; // System.gc();
return false;
}
public int getCurrentRecordID (RMSStore rmsTable, String Rec)
{
RecordEnumeration re = null;
try
{
re = rmsTable.getRecordEnumData();
while (re.hasNextElement())
{
int id = re.nextRecordId();
String record = rmsTable.getRecordFromId(id);
if (record.indexOf(Rec) != -1)
{
return id;
}
}
}
catch (Exception e) { System.out.println ("getCurrentRecordID Error:" + e.toString()); }
return -1;
}
public int writeRecord(String str)
{
int id = 0;
try
{
id = rs.addRecord(str.getBytes(), 0, str.getBytes().length);
}
catch (RecordStoreFullException e)
{
CustomAlert memoryFullAlert = new CustomAlert("");
memoryFullAlert.setString("Memory Full");
MyMidlet.getDisplay().setCurrent(memoryFullAlert);
}
catch (Exception e)
{
e.printStackTrace();
}
return id;
}
public int writeByteRecord(byte[] data)
{
int id = -1;
try
{
id = rs.addRecord(data, 0, data.length);
}
catch (RecordStoreFullException e)
{
e.printStackTrace();
CustomAlert memoryFullAlert = new CustomAlert("");
memoryFullAlert.setString("Memory Full");
MyMidlet.getDisplay().setCurrent(memoryFullAlert);
}
catch (Exception e)
{
e.printStackTrace();
}
return id;
}
public int getRecordCount()
{
try
{
return rs.getNumRecords();
}
catch (Exception e)
{
e.printStackTrace();
}
return 0;
}
public byte[] getRecordDataFromId(int id)
{
byte[] data = null;
try
{
data = rs.getRecord(id);
}
catch (Exception e)
{
e.printStackTrace();
}
return data;
}
public String getRecordFromId(int id)
{
return new String(getRecordDataFromId(id));
}
public byte[] getRecordByteFromId(int id)
{
return getRecordDataFromId(id);
}
public void deleteRecord(int id)
{
try
{
rs.deleteRecord(id);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public boolean checkRecordExists(String compare)
{
for(int i = 0; i < getRecordCount(); i++)
{
if(compare.equals(getRecordFromId(i + 1)))
{
return true;
}
}
return false;
}
public int getMaxRMSSize()
{
int size = 0;
try
{
size = rs.getSizeAvailable() + rs.getSize();
}
catch (RecordStoreNotOpenException e)
{
e.printStackTrace();
}
return size;
}
public void setRecordById(String str, int id)
{
try
{
rs.setRecord(id, str.getBytes(), 0, str.getBytes().length);
}
catch (Exception e)
{
e.printStackTrace();
}
}
public int getNextRecordId()
{
int id = 0;
try
{
id = rs.getNextRecordID();
}
catch (Exception e)
{
e.printStackTrace();
}
return id;
}
public RecordEnumeration getRecordEnumData()
{
try
{
return rs.enumerateRecords(null, null, false);
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
public String [] getRecordData()
{
String[] str = null;
int counter = 0;
try
{
RecordEnumeration enumeration = rs.enumerateRecords(null, null, false);
str = new String[rs.getNumRecords()];
while(enumeration.hasNextElement())
{
try
{
str[counter] = (new String(enumeration.nextRecord()));
counter ++;
}
catch (Exception e)
{
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
1
RMS是基於記錄的數據存儲機制,因此您可以非常輕鬆地存儲多條記錄,請參閱以下博客以瞭解RMS的工作原理。
相關問題
- 1. j2me midp 1.0中的本地數據庫
- 2. J2ME中的數據庫連接
- 3. j2me數據庫選項,jmesql
- 4. J9 JVM上的數據庫(J2ME)
- 5. J2ME的數據庫和UI框架?
- 6. 在j2me中的derby數據庫中的存儲數據的位置
- 7. J2ME的Telnet庫
- 8. 存儲在J2ME中的數據庫加密對象
- 9. 將數據從servlet保存到j2me中的記錄庫
- 10. j2me中的數據存儲選項
- 11. 如何在j2me中爲S40開發製作數據庫選項?
- 12. 如何在j2me中從SD卡訪問數據庫文件
- 13. 使用J2me在RMS數據庫中創建多列
- 14. 如何在j2me中連接本地數據庫
- 15. 在j2me中創建庫
- 16. j2me發佈數據限制
- 17. 用j2me發送數據報
- 18. J2ME小部件庫
- 19. 連接網絡服務器與數據庫和J2ME
- 20. 如何使用J2ME連接到任何數據庫?
- 21. 如何刪除J2ME應用程序中的所有本地數據庫
- 22. 如何連接j2me應用程序中的數據庫諾基亞s40平臺
- 23. MySql數據庫中的Wordpress數據庫?
- 24. 在數據庫中的數據庫
- 25. J2ME中的SimpleDateFormat
- 26. j2me中的NoClassDefFoundError
- 27. J2ME中的DateField
- 28. 如何在J2ME中隱藏列表中的數據/字段
- 29. 如何將數據存儲在j2me中的文件中?
- 30. Howto Base64解碼(庫)在J2ME中?
Lucifer先生,請爲我提供CustomAlert和MyMidlet文件,因此我可以直觀地理解它。 – Karan 2012-07-23 05:08:55
你可以評論那部分,它只能用於顯示Alert :) – Lucifer 2012-07-23 05:09:46