记录应用程序 : 数据库持续性 « 基于J2ME « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » 基于J2ME » 数据库持续性屏幕截图 
记录应用程序
记录应用程序

/*
 * Wireless Java 2nd edition Jonathan Knudsen Publisher: Apress ISBN: 1590590775
 */

import java.util.Enumeration;
import java.util.Hashtable;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;

public class RecordMIDlet extends MIDlet implements CommandListener {
  private static final String kUser = "user";

  private static final String kPassword = "password";

  private Preferences mPreferences;

  private Form mForm;

  private TextField mUserField, mPasswordField;

  public RecordMIDlet() {
    try {
      mPreferences = new Preferences("preferences");
    catch (RecordStoreException rse) {
      mForm = new Form("Exception");
      mForm.append(new StringItem(null, rse.toString()));
      mForm.addCommand(new Command("Exit", Command.EXIT, 0));
      mForm.setCommandListener(this);
      return;
    }

    mForm = new Form("Login");
    mUserField = new TextField("Name", mPreferences.get(kUser)320);
    mPasswordField = new TextField("Password", mPreferences.get(kPassword),
        320);
    mForm.append(mUserField);
    mForm.append(mPasswordField);

    mForm.addCommand(new Command("Exit", Command.EXIT, 0));
    mForm.setCommandListener(this);
  }

  public void startApp() {
    Display.getDisplay(this).setCurrent(mForm);
  }

  public void pauseApp() {
  }

  public void destroyApp(boolean unconditional) {
    // Save the user name and password.
    mPreferences.put(kUser, mUserField.getString());
    mPreferences.put(kPassword, mPasswordField.getString());
    try {
      mPreferences.save();
    catch (RecordStoreException rse) {
    }
  }

  public void commandAction(Command c, Displayable s) {
    if (c.getCommandType() == Command.EXIT) {
      destroyApp(true);
      notifyDestroyed();
    }
  }
}

class Preferences {
  private String mRecordStoreName;

  private Hashtable mHashtable;

  public Preferences(String recordStoreNamethrows RecordStoreException {
    mRecordStoreName = recordStoreName;
    mHashtable = new Hashtable();
    load();
  }

  public String get(String key) {
    return (StringmHashtable.get(key);
  }

  public void put(String key, String value) {
    if (value == null)
      value = "";
    mHashtable.put(key, value);
  }

  private void load() throws RecordStoreException {
    RecordStore rs = null;
    RecordEnumeration re = null;

    try {
      rs = RecordStore.openRecordStore(mRecordStoreName, true);
      re = rs.enumerateRecords(null, null, false);
      while (re.hasNextElement()) {
        byte[] raw = re.nextRecord();
        String pref = new String(raw);
        // Parse out the name.
        int index = pref.indexOf('|');
        String name = pref.substring(0, index);
        String value = pref.substring(index + 1);
        put(name, value);
      }
    finally {
      if (re != null)
        re.destroy();
      if (rs != null)
        rs.closeRecordStore();
    }
  }

  public void save() throws RecordStoreException {
    RecordStore rs = null;
    RecordEnumeration re = null;
    try {
      rs = RecordStore.openRecordStore(mRecordStoreName, true);
      re = rs.enumerateRecords(null, null, false);

      // First remove all records, a little clumsy.
      while (re.hasNextElement()) {
        int id = re.nextRecordId();
        rs.deleteRecord(id);
      }

      // Now save the preferences records.
      Enumeration keys = mHashtable.keys();
      while (keys.hasMoreElements()) {
        String key = (Stringkeys.nextElement();
        String value = get(key);
        String pref = key + "|" + value;
        byte[] raw = pref.getBytes();
        rs.addRecord(raw, 0, raw.length);
      }
    finally {
      if (re != null)
        re.destroy();
      if (rs != null)
        rs.closeRecordStore();
    }
  }
}



           
       
Related examples in the same category
1. Write Read Mixed Data Types Example Write Read Mixed Data Types Example
2. Record Enumeration Example Record Enumeration Example
3. Mixed Record Enumeration Example Mixed Record Enumeration Example
4. 排序记录示例
5. Sort Mixed Record Data Type Example Sort Mixed Record Data Type Example
6. 搜索示例搜索示例
7. 搜索好坏参半的记录数据类型示例搜索好坏参半的记录数据类型示例
8. 存储数据库存储数据库
9. Test the RMS listener methodsTest the RMS listener methods
10. Use streams to read and write Java data types to the record store.Use streams to read and write Java data types to the record store.
11. 读取和写入记录。读取和写入记录。
12. 持久性排序程序
13. 持久性:存储和显示游戏得分
14. 阅读显示文件阅读显示文件
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.