使用Android中数据库来模拟MIDP中的RMS
来源:互联网 发布:批处理开启端口 编辑:程序博客网 时间:2024/05/24 02:32
废话不说了,直接列代码。虽然不是很完整,给大家参考一下吧。
package JavaME.rms;
import java.io.File;
import JavaME.midlet.MIDlet;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
public class RecordStore
{
private static final String DATA_NAME = "rms.db";
private static final String ID = "id";
private static final String VALUE = "value";
private static final String TABLE = "rms";
private static final String TABLE_NAME = "name";
public static final int AUTHMODE_ANY = 0;
public static final int AUTHMODE_PRIVATE = 1;
private static String s_sDataPath = null;
private String m_sRmsName = null;
private SQLiteDatabase m_oData = null;
private RecordStore(String sName,boolean bCreate)throws Exception
{
if(sName != null && sName.length() > 0)
{
createDir();
m_oData = SQLiteDatabase .openOrCreateDatabase(s_sDataPath + "/" + DATA_NAME, null);
m_sRmsName = sName;
try
{
m_oData.rawQuery("SELECT * FROM " + TABLE, null);
}
catch(Exception e)
{
m_oData.execSQL("CREATE TABLE " + TABLE + " (" + TABLE_NAME + " TEXT)");
}
//检查是否已经存在该表
try
{
m_oData.rawQuery("SELECT * FROM " + m_sRmsName, null);
}
catch(Exception e)
{
if(bCreate)
{
m_oData.execSQL("CREATE TABLE " + m_sRmsName +" (" + ID + " INTEGER PRIMARY KEY," + VALUE +" COLLATE BINARY)");
m_oData.execSQL("INSERT INTO " + TABLE + "(" + TABLE_NAME + ") VALUES('" + m_sRmsName + "')");
}
else
{
throw new RecordStoreNotFoundException();
}
}
}
else
{
throw new RecordStoreException();
}
}
public static void init(MIDlet oMidLet)
{
s_sDataPath = oMidLet.getFilesDir().getPath();
}
public static RecordStore openRecordStore(String sName,boolean bCreate)throws Exception
{
RecordStore oRecord = new RecordStore(sName,bCreate);
return oRecord;
}
public static RecordStore openRecordStore(String sName,boolean bCreate,int wAnthomde,boolean writable)
{
return null;
}
public static RecordStore openRecordStore(String sName,String sVendorName,String sSuiteName)
{
return null;
}
public static void deleteRecordStore(String sRecordStore)throws Exception
{
SQLiteDatabase oData = null;
createDir();
try
{
oData = SQLiteDatabase.openOrCreateDatabase(s_sDataPath + "/" + DATA_NAME, null);
oData.execSQL("DROP TABLE " + sRecordStore);
oData.execSQL("DELETE FROM " + TABLE + " WHERE " + TABLE_NAME + "='" + sRecordStore + "'");
}
catch(Exception e)
{
throw new RecordStoreNotFoundException();
}
finally
{
if(oData != null)
{
oData.close();
}
}
}
public int addRecord(byte[] bzData, int wOffset, int wNumBytes)throws Exception
{
int wId = -1;
if(m_oData != null)
{
if(wOffset >= 0 && wNumBytes > 0)
{
byte[] bzMid = null;
if(wOffset == 0 && wNumBytes == bzData.length)
{
bzMid = bzData;
}
else
{
bzMid = new byte[wNumBytes];
for(int i = 0;i < wNumBytes;++i)
{
bzMid[i] = bzData[wOffset + i];
}
}
ContentValues oValues = new ContentValues();
oValues.put(VALUE, bzMid);
try
{
//写入新数据
m_oData.insert(m_sRmsName, null, oValues);
//获取最后记录的id
Cursor oCursor = m_oData.rawQuery("SELECT * FROM " + m_sRmsName, null);
oCursor.moveToLast();
wId = oCursor.getInt(oCursor.getColumnIndex(ID));
}
catch(Exception e)
{
throw new RecordStoreException();
}
}
}
else
{
throw new RecordStoreNotOpenException();
}
return wId;
}
public void setRecord(int wRecordId,byte[] bzData,int wOffset,int wNumBytes)throws Exception
{
if(m_oData != null)
{
if(bzData != null && wOffset >= 0 && wNumBytes > 0)
{
ContentValues oValues = new ContentValues();
oValues.put(VALUE, bzData);
m_oData.update(m_sRmsName, oValues, "" + ID + "=" + wRecordId, null);
}
}
else
{
throw new RecordStoreNotOpenException();
}
}
public void closeRecordStore()
{
if(m_oData != null)
{
m_oData.close();
m_oData = null;
}
}
public void deleteRecord(int wRecordId)throws Exception
{
if(m_oData != null)
{
try
{
m_oData.execSQL("DELETE FROM " + m_sRmsName + " WHERE " + ID + "=" + wRecordId);
}
catch(Exception e)
{
throw new RecordStoreException();
}
}
else
{
throw new RecordStoreNotOpenException();
}
}
public int getNextRecordID()throws Exception
{
if(m_oData != null)
{
try
{
Cursor oCursor = m_oData.rawQuery("SELECT * FROM " + m_sRmsName, null);
if(oCursor.moveToLast())
{
return oCursor.getInt(oCursor.getColumnIndex(ID) + 1);
}
else
{
return 1;
}
}
catch(Exception e)
{
throw new RecordStoreException();
}
}
else
{
throw new RecordStoreNotOpenException();
}
}
public int getNumRecords()throws Exception
{
if(m_oData != null)
{
try
{
Cursor oCursor = m_oData.rawQuery("SELECT * FROM " + m_sRmsName, null);
return oCursor.getCount();
}
catch(Exception e)
{
throw new RecordStoreException();
}
}
else
{
throw new RecordStoreNotOpenException();
}
}
public byte[] getRecord(int wRecordId)throws Exception
{
byte[] bzBuf = null;
if(m_oData != null)
{
if(wRecordId > 0)
{
try
{
Cursor oCursor = m_oData.rawQuery("SELECT * FROM " + m_sRmsName + " WHERE " + ID + "=" + wRecordId, null);
if(oCursor.moveToFirst())
{
bzBuf = oCursor.getBlob(oCursor.getColumnIndex(VALUE));
}
}
catch(Exception e)
{
throw new RecordStoreException();
}
}
else
{
throw new InvalidRecordIDException();
}
}
else
{
throw new RecordStoreNotOpenException();
}
return bzBuf;
}
public int getRecord(int wRecordId,byte[] bzBuf,int wOffset)throws Exception
{
if(bzBuf != null)
{
if(wOffset >= 0)
{
byte[] bzMid = getRecord(wRecordId);
int wCount = bzBuf.length;
if(wCount > bzMid.length - wOffset)
{
wCount = bzMid.length - wOffset;
}
for(int i = 0;i < wCount; ++i)
{
bzBuf[i] = bzMid[wOffset + i];
}
}
else
{
throw new ArrayIndexOutOfBoundsException();
}
}
else
{
throw new RecordStoreNotOpenException();
}
return 0;
}
public int getRecordSize(int wRecordId)throws Exception
{
byte[] bzBuf = getRecord(wRecordId);
if(bzBuf != null)
{
return bzBuf.length;
}
else
{
return 0;
}
}
public long getSize()throws Exception
{
if(m_oData != null)
{
File oFile = new File(s_sDataPath + "/" + DATA_NAME);
return oFile.length();
}
else
{
throw new RecordStoreNotOpenException();
}
}
public long getSizeAvailable()throws Exception
{
if(m_oData != null)
{
return m_oData.getMaximumSize();
}
else
{
throw new RecordStoreNotOpenException();
}
}
public int getVersion()
{
if(m_oData != null)
{
return m_oData.getVersion();
}
return -1;
}
public String getName()
{
return m_sRmsName;
}
private static void createDir()
{
File oDir = new File(s_sDataPath);
if(!oDir.exists())
{
oDir.mkdir();
}
}
public static String[] listRecordStores()throws Exception
{
SQLiteDatabase oData = null;
createDir();
String[] szBuf = null;
try
{
oData = SQLiteDatabase.openOrCreateDatabase(s_sDataPath + "/" + DATA_NAME, null);
Cursor oCursor = oData.rawQuery("SELECT * FROM " + TABLE, null);
if(oCursor.getCount() > 0)
{
szBuf = new String[oCursor.getCount()];
int i = 0;
if(oCursor.moveToFirst())
{
do
{
szBuf[i++] = oCursor.getString(oCursor.getColumnIndex(TABLE_NAME));
}while(oCursor.moveToNext());
}
}
}
catch(Exception e)
{
throw e;
}
finally
{
if(oData != null)
{
oData.close();
}
}
return szBuf;
}
}
- 使用Android中数据库来模拟MIDP中的RMS
- 简单封装MIDP RMS操作
- J2ME学习笔记(八)-----处理MIDP RMS
- 使用H2数据库来模拟进行单元测试
- J2ME中RMS的使用解析
- J2ME中RMS的使用解析
- J2ME中RMS的使用解析
- J2ME中RMS的使用解析
- 使用Django来清理数据库中的数据
- 数据库和 MIDP
- 使用mouse_event来模拟一次选中文本框中的文本
- 使用mouse_event来模拟一次选中文本框中的文本
- 使用mouse_event来模拟一次选中文本框中的文本
- 使用mouse_event来模拟一次选中文本框中的文本
- MIDP中的动画
- J2ME RMS数据库操作
- Android中使用SQLiteOpenHelper管理SD卡中的数据库
- Android中使用SQLiteOpenHelper管理SD卡中的数据库
- 构建ARM-PC无线局域网->Tp-link wn321G无线网卡(Rt73)驱动+wireless_tools for Kernel 2.6
- 1752年9月 少了的12天
- Eclipse RCP程序更改原始图标
- 与大学计算机有关的日子
- const用法详解
- 使用Android中数据库来模拟MIDP中的RMS
- JDBC参数设置
- Domino调色板
- [转]屏幕保护色的设置
- C#的des加密解密
- org.apache.commons.lang.StringUtil的使用(转帖)
- 图的深度优先搜索算法和广度优先搜索算法
- C# 生成各种格式各种大小的图片类
- 针对ASP.NET页面实时进行GZIP压缩优化的几款压缩模块的使用简介及应用测试!(附源码)