Record Management System from Getting Started to Four Works Series

zhaozj2021-02-16  70

This article will mainly describe the usage of Recordfilter and Recordenumeration, which is critical in the entire Record Management System. Since I feel that RecordComparator and Recordfilter are similar and have been relatively small, they will not be introduced here. We are still how to use these two interfaces through a demonstration application.

Recordfilter's definition is very simple, he only defines a method that is Boolean Matches (Byte [] Data). Public interface recordfilter {boolean matches (Byte [] Recorddata);} It is also very simple to use, we only need to implement this method and return to the value of the Boolean type as needed, usually we use this interface when looking up the record, It is passed as a parameter to the EnumerateRecords () method, such as Recordenumeration Records = RS.EnumeRerecords (New RecordryFilter (Key), Null, False), first we look at the parameters of this method, the first parameter is Recordfilter, it is used Filtering the record in the database is the method we defined above. Boolean matches (byte [] data), the second parameter is RecordComparator, which is sorted for the selected data, if you don't specify Sort by default order. The third parameter is a boolean type. If it is true, then Record will track data change in RMS, which is more expensive, I usually use false. After obtaining records we can make a lot of useful operations, the specific method is: public interface RecordEnumeration {void destroy (); boolean hasNextElement (); boolean hasPreviousElement (); boolean isKeptUpdated (); void keepUpdated (boolean keepUpdated); byte [] nextRecord () throws InvalidRecordIDException, RecordStoreNotOpenException, RecordStoreException; int nextRecordId () throws InvalidRecordIDException; int numRecords (); byte [] previousRecord () throws InvalidRecordIDException, RecordStoreNotOpenException, RecordStoreException; int previousRecordId () throws InvalidRecordIDException; void rebuild (); void reset ( }

The method of marking is often used in mind. When we implement Recordfilter, it is usually written into an internal class of a class, which is very reasonable. My procedure below still uses this method, Private Static Class Recordryfilter Implements Recordfilter {Private String Key;

public RecordEntryFilter (String key) {this.key = key;} public boolean matches (byte [] data) {try {return RecordEntry.matches (data, key);} catch (IOException e) {e.printStackTrace (); return False;}}} In an example, we store several data entities to RMS, which includes a UserName and a phonenumber field. When we write and read the fields, you can use the serialization mechanisms in the series. Import java.io. *; / * * Created on 2004-7-13 * * Todo to change the template for this generated file go to * window - preferences - java - code style - code templates * /

/ ** * @author E2412C * * TODO To change the template for this generated type comment go to Window - * Preferences - Java - Code Style - Code Templates * / public class RecordEntry {private String userName; private String phoneNum;

Public Recordentry () {}

Public Recordentry (String Username, String Phonenum) {this.usename = Username; this.phonenum = phonenum;

/ ** * @Return Returns the phonenum. * / Public string getphonenum () {Return Phonenum;

/ ** * @Param phonenum * the phonenum to set. * / Public void setphonenum (String phoneenum) {this.phonenum = phonenum;

/ ** * @Return Returns the username. * / Public string getUsername () {return username;}

/ ** * @Param username * The username to set. * / Public void setusername (String username) {this.username = username;

public byte [] serialize () throws IOException {ByteArrayOutputStream baos = new ByteArrayOutputStream (); DataOutputStream dos = new DataOutputStream (baos); dos.writeUTF (userName); dos.writeUTF (phoneNum);

Return baos.tobytearray ();

public static RecordEntry deserialize (byte [] data) throws IOException {ByteArrayInputStream bais = new ByteArrayInputStream (data); DataInputStream dis = new DataInputStream (bais); RecordEntry record = new RecordEntry (); record.userName = dis.readUTF (); record .phoneNum = dis.readUTF (); return record;} public static boolean matches (byte [] data, String key) throws IOException {ByteArrayInputStream bais = new ByteArrayInputStream (data); DataInputStream dis = new DataInputStream (bais); return dis. Readutf (). Equals (key);} public string toString () {Return UserName ":" phoneenum;}}

Another focus on our paper is how to use the Recordenumeration, the relevant code is written in a RecordModel class as follows: import java.io ioException;

Import javax.microedition.rms. *; / * * Created on 2004-7-13 * * Todo to change the template for this generated file go to * window - preferences - java - code style - code templates * /

/ ** * @author E2412C * * TODO To change the template for this generated type comment go to Window - * Preferences - Java - Code Style - Code Templates * / public class RecordModel {private RecordStore rs; private boolean firstTime = true; public Static final string name = "record"; private recording [] record = {new recording ("ming", "12345"), New Recordentry ("Pain", "123456"), New Recordentry ("Linux", "234566" NEW Recordentry ("Mingtian", "3456677")}; private static class recording refilter imports recordfilter {private string key

Public RecordiffEnTryfilter (String Key) {this.key =

Public boolean matches (byte {return) {ooException e) {E.PrintStackTrace (); returnaftractrac (}}}}

Public RecordModel () {Try {RS = RecordStore.OpenRecordStore (Name, true);} catch (recordstoreException e) {E.PrintStackTrace ();} if (firsttime) {init (); firsttime = false;}}

Public void init () {try = 0; I

}}} Catch (ioException e) {E.PrintStackTrace ();} catch (recordstoreException E) {E.PrintStackTrace ();}}

public RecordEntry [] getRecord (String key) {try {if (rs.getNumRecords ()> 0) {RecordEnumeration records = rs.enumerateRecords (new RecordEntryFilter (key), null, false); int length = records.numRecords (); IF (length == 0) {return new recording [0];} else {system.out.println (length); recording = new recording [longth]; for (int i = 0; i

/ ** * @author E2412C * * TODO To change the template for this generated type comment go to Window - * Preferences - Java - Code Style - Code Templates * / public class RecordFilterMIDlet extends MIDlet implements ItemStateListener {private Display display; private Form mainForm ; private TextField textField; private RecordModel model; / * * (non-Javadoc) * * @see javax.microedition.midlet.MIDlet # startApp () * / protected void startApp () throws MIDletStateChangeException {// TODO Auto-generated method stub Display = display.getdisplay (this); mainform = new form ("test"); model = new recordmodel (); TextField = New TextField ("Input:", NULL, 20, TextField.Append (TextField) ); Mainform.SetItemStatelistener (this); Display.SetCurrent (Mainform);

}

/ * * (Non-javadoc) * * @see javax.microedition.midlet.midlet # Pauseapp () * / protected void Pauseapp () {// Todo auto-generated method stub

}

/ * * (Non-Javadoc) * * @see javax.microedition.midlet.MIDlet # destroyApp (boolean) * / protected void destroyApp (boolean arg0) throws MIDletStateChangeException {// TODO Auto-generated method stub

}

public void itemStateChanged (Item item) {if (item == textField) {String input = textField.getString (); RecordEntry [] record = model.getRecord (input); if (record.length == 0) {System.out .println ("no record");} else {

For (int i = 0; i

转载请注明原文地址:https://www.9cbs.com/read-16382.html

New Post(0)