Enterprise J2ME Developing Mobile Java Applications [Electronic resources] نسخه متنی

اینجــــا یک کتابخانه دیجیتالی است

با بیش از 100000 منبع الکترونیکی رایگان به زبان فارسی ، عربی و انگلیسی

Enterprise J2ME Developing Mobile Java Applications [Electronic resources] - نسخه متنی

Michael Juntao Yuan

| نمايش فراداده ، افزودن یک نقد و بررسی
افزودن به کتابخانه شخصی
ارسال به دوستان
جستجو در متن کتاب
بیشتر
تنظیمات قلم

فونت

اندازه قلم

+ - پیش فرض

حالت نمایش

روز نیمروز شب
جستجو در لغت نامه
بیشتر
لیست موضوعات
توضیحات
افزودن یادداشت جدید



12.3 The IBM DB2e FastRecordStore


IBM DB2e (v8.1) provides a FastRecordStore class over the MIDP standard RMS record store. FastRecordStore packs several database rows into one RMS record and supports indexes for fast lookup. This results in much improved performance over the linear RMS record store.

To create a database table in a FastRecordStore, we have to first create the table in a DB2e backend database. Then, we synchronize the table to the MIDP device using IBM DB2e Sync. After the synchronization, a FastRecordStore with the same name as the backend table is created. We can now read or update data in the FastRecordStore via the TableMetaData class. All the changes we make on the MIDP client will be sent back to the backend table upon the next synchronization operation. For more information about database synchronization and the IBM Sync, please refer to Chapter 13, Section 13.3.2.

Note

We cannot start with record stores on the MIDP client first, as they will be deleted on the first synchronization.

In the FastRecordStore, each table row is packed into a byte array. Developers are required to manipulate those raw arrays manually. All rows start with a dirty byte and are followed by data in each column according to the following rules.

If the column is not nullable, the data for the column is present.

If the column is nullable there is a 1-byte boolean null indicator. If the indicator is true, no data follows. Otherwise, the column data follows.


To illustrate the above points, the SQL INSERT statement in Listing 12.3 can be interpreted as the Java code in Listing 12.4.

Listing 12.3. SQL script for a sample table



create table MyTable (a int, b int not null, c varchar(20),
id bigint not null primary key);
insert into MyTable values (null, 10, 'Have fun', 99);

Listing 12.4. Java code for the INSERT statement in Listing 12.3


DataOutputStream dout = new DataOutputStream(byteArrayOutStrm);
// The dirty byte
dout.writeByte(0);
// 'a' is null, no data follows
dout.writeBoolean(true);
// 'b', not nullable
// So there is no null indicator
dout.writeInt(10);
// 'c' is nullable, but not null
dout.writeBoolean(false);
dout.writeUTF("Have fun");
// 'id' not nullable
dout.writeLong(99);

The following code (Listing 12.5) demonstrates how to iterate through rows, delete a row, and read out data in a FastRecordStore.

Listing 12.5. Browse the FastRecordStore



TableMetaData rs = ((MIDPISync)isync).getTableMetaDataByName(sName);
int numCols = rs.getNumCols();
FastRecordStore rms = FastRecordStore.openRecordStore(sName, false);
Index index = new Index(rms, rs);
FastRecordEnumeration enum = rms.enumerateRecords(null, null, false);
// Iterate through rows
while (enum.hasNextElement()) {
int id = enum.nextRecordId();
int recSize = rms.getRecordSize(id);
byte data = new byte[recSize];
recSize = rms.getRecord(id, data, 0);
// Example: delete a row
if (id == rowToDelete) {
data[0] = ISync.ROW_DELETED;
index.updateRecord(id, data, recSize);
continue;
}
// Read out each field for each row
ByteArrayInputStream bin = new ByteArrayInputStream(data);
DataInputStream din = new DataInputStream(bin);
din.readByte(); // dirty byte
for (int c = 0; c < numCols; c++) {
int type = rs.getType(c);
boolean isNullable = rs.isNullable(c);
boolean ind = false;
if (isNullable) ind = din.readBoolean();
if (ind) continue;
switch (type) {
case VARCHAR:
strval = din.readUTF();
break;
case INTEGER:
intval = din.readInt();
break;
// more ...
}
}
}


/ 204