Presentation is loading. Please wait.

Presentation is loading. Please wait.

모바일 자바 프로그래밍 MIDP RMS Ps lab 오민경. MIDP RMS  RMS (Record Management System)  MIDP 에서 정의하는 영속성을 지닌 자체 데이터 저장 공간  Record Store 의 집합으로 구성된 아주 간단한 데이터베이스.

Similar presentations


Presentation on theme: "모바일 자바 프로그래밍 MIDP RMS Ps lab 오민경. MIDP RMS  RMS (Record Management System)  MIDP 에서 정의하는 영속성을 지닌 자체 데이터 저장 공간  Record Store 의 집합으로 구성된 아주 간단한 데이터베이스."— Presentation transcript:

1 모바일 자바 프로그래밍 MIDP RMS Ps lab 오민경

2 MIDP RMS  RMS (Record Management System)  MIDP 에서 정의하는 영속성을 지닌 자체 데이터 저장 공간  Record Store 의 집합으로 구성된 아주 간단한 데이터베이스  쓰레드에 대해 안전하게 구현되어 있음 ( 여러 개의 쓰레드가 동시에 수행되는 경우, 순차적으로 수행 )  RMS 의 구성요소  Record Store : 각 Record Store 는 이름으로 구분  Record : 하나의 Record 는 바이트 배열로 구성

3 RMS 사용  RecordStore 객체 열기  static RecordStore openRecordStore (String RecordStoreName, boolean createIfNecessary);  RecordStore 객체 닫기  void closeRecordStore();  RecordStore 내의 레코드에 대한 인덱싱하기  RecordEnumeration 인터페이스를 이용하여 RecordStore 내의 레코드들을 훑어 볼 수 있다.  RecordEnumeration enumerateRecords(RecordFilter filter, RecordComparator comparator, boolean keepUpdated);

4 RMS 사용 (cont.)  RecordStore 에 레코드 추가  int addRecord(byte[] data, int offset, int numBytes);  RecordStore 내의 레코드 삭제  void deleteRecord(int recordId);  RecordStore 내의 레코드 열람하기  byte[] getRecord(int recordId);

5 주소록 예제  import java.io.*;  import java.util.*;  import javax.microedition.midlet.*;  import javax.microedition.lcdui.*;  import javax.microedition.rms.*;  public class AddrBookMIDlet extends MIDlet  implements CommandListener  {  private Display display;  private List listAddress;  private Form formAdd;  private RecordStore db;  private Alert alertView;  private Vector idlist;  public AddrBookMIDlet()  {  // 주소록 List 객체 생성  listAddress = new List("Address Book", List.IMPLICIT);  listAddress.addCommand(new Command("View", Command.ITEM, 0));  listAddress.addCommand(new Command("Add", Command.SCREEN, 1));  listAddress.addCommand(new Command("Remove", Command.ITEM, 1));

6 주소록 예제 (cont.)  listAddress.addCommand(new Command("Exit", Command.EXIT, 1));  listAddress.setCommandListener(this);  // 주소록 추가 Form 객체 생성  formAdd = new Form("Add Address");  formAdd.append(new TextField("Name", "", 10, TextField.ANY));  formAdd.append(new TextField("E-Mail", "", 32, TextField.EMAILADDR));  formAdd.append(new TextField("Address", "", 64, TextField.ANY));  formAdd.append(new TextField("Tel", "", 13, TextField.PHONENUMBER));  formAdd.addCommand(new Command("OK", Command.OK, 0));  formAdd.addCommand(new Command("Cancel", Command.CANCEL, 0));  formAdd.setCommandListener(this);  // 주소록 출력 Alert 객체 생성  alertView = new Alert("");  alertView.setTimeout(Alert.FOREVER);  }  public void startApp() {  display = Display.getDisplay(this);  db = openDB();  loadDB(db);  display.setCurrent(listAddress);  }

7 주소록 예제 (cont.)  public void pauseApp() {  try {  db.closeRecordStore();  } catch(Exception e) {}  }  public void destroyApp(boolean unconditional) {  try {  db.closeRecordStore();  } catch(Exception e) {}  }  public RecordStore openDB() {  RecordStore rs;  try {  rs = RecordStore.openRecordStore("addrbook", true);  } catch(RecordStoreException ex) {  rs = null;  }  return rs;  }

8 주소록 예제 (cont.)  /**  * RMS 에서 주소록 정보를 모두 읽어들입니다.  */  public boolean loadDB(RecordStore rs) {  // 리스트를 비운다.  while(listAddress.size()>0)  listAddress.delete(0);  idlist = new Vector();  // 주소록 레코드 저장소 내의 레코드 나열을 얻습니다.  RecordEnumeration enum = null;  try {  enum = rs.enumerateRecords(null, null, false);  } catch(RecordStoreNotOpenException ex) {  return false;  }  // 레코드 나열에서 각 레코드의 ID 를 읽어서 저장한다.  while(enum.hasNextElement()) {  try {  int id = enum.nextRecordId();  idlist.addElement(new Integer(id));  } catch(Exception ex) { }  }  enum.destroy();

9 주소록 예제 (cont.)  // 각 레코드를 읽어 리스트에 등록합니다.  for (int i=0; i<idlist.size(); i++) {  String name = null;  try {  int id = ((Integer)idlist.elementAt(i)).intValue();  byte[] record = rs.getRecord(id);  ByteArrayInputStream bais = new ByteArrayInputStream(record);  DataInputStream dis = new DataInputStream(bais);  name = dis.readUTF();  dis.close();  } catch(Exception ex) {  name = null;  }  if (name==null)  idlist.removeElementAt(i);  else  listAddress.append(name, null);  }  return true;  }

10 주소록 예제 (cont.)  /**  * 레코드 저장소에 새로운 주소를 추가합니다.  */  public boolean addAddress(RecordStore rs, String name, String email,  String address, String tel)  {  try {  ByteArrayOutputStream baos = new ByteArrayOutputStream();  DataOutputStream dos = new DataOutputStream(baos);  dos.writeUTF(name);  dos.writeUTF(email);  dos.writeUTF(address);  dos.writeUTF(tel);  byte[] b = baos.toByteArray();  int id = rs.addRecord(b, 0, b.length);  idlist.addElement(new Integer(id));  listAddress.append(name, null);  dos.close();  } catch(Exception ex) {  return false;  }  return true;  }

11 주소록 예제 (cont.)  /**  * 레코드 저장소에서 주소를 삭제합니다.  */  public boolean removeAddress(RecordStore rs, int index)  {  try {  int id = ((Integer)idlist.elementAt(index)).intValue();  rs.deleteRecord(id);  idlist.removeElementAt(index);  listAddress.delete(index);  } catch(Exception ex) {  return false;  }  return true;  }  /**  * 레코드 저장소에서 주소를 읽어옵니다.  */  public String viewAddress(RecordStore rs, int index)  {  String result = null;  try {  int id = ((Integer)idlist.elementAt(index)).intValue();  byte[] record = rs.getRecord(id);

12 주소록 예제 (cont.)  ByteArrayInputStream bais = new ByteArrayInputStream(record);  DataInputStream dis = new DataInputStream(bais);  StringBuffer strbuf = new StringBuffer();  // 이름 읽어들이기  strbuf.append("NAME : \n");  strbuf.append(dis.readUTF());  strbuf.append("\n");  // 메일주소 읽어들이기  strbuf.append("E-MAIL : \n");  strbuf.append(dis.readUTF());  strbuf.append("\n");  // 주소 읽어들이기  strbuf.append("Address : \n");  strbuf.append(dis.readUTF());  strbuf.append("\n");  // 전화번호 읽어들이기  strbuf.append("Tel : \n");  strbuf.append(dis.readUTF());  strbuf.append("\n");

13 주소록 예제 (cont.)  dis.close();  result = strbuf.toString();  } catch(Exception ex) {  result = null;  }  return result;  }  public void commandAction(Command c, Displayable s) {  if (s == listAddress) { // 현재 화면이 List 이면  if (c == List.SELECT_COMMAND || c.getLabel().equals("View")) {  // 선택된 주소록 출력  String address = viewAddress(db, listAddress.getSelectedIndex());  alertView.setString(address);  display.setCurrent(alertView);  } else if (c.getLabel().equals("Add")) {  // 폼 초기화  ((TextField)formAdd.get(0)).setString("");  ((TextField)formAdd.get(1)).setString("");  ((TextField)formAdd.get(2)).setString("");  ((TextField)formAdd.get(3)).setString("");  display.setCurrent(formAdd);

14 주소록 예제 (cont.)  } else if (c.getLabel().equals("Remove")) {  removeAddress(db, listAddress.getSelectedIndex());  } else if (c.getLabel().equals("Exit")) {  // 프로그램 종료  destroyApp(false);  notifyDestroyed();  }  }  else if (s == formAdd) { // 현재 화면이 Form 이면  if (c.getLabel().equals("OK")) {  addAddress(db, ((TextField)formAdd.get(0)).getString(),  ((TextField)formAdd.get(1)).getString(),  ((TextField)formAdd.get(2)).getString(),  ((TextField)formAdd.get(3)).getString());  display.setCurrent(listAddress);  }  if (c.getLabel().equals("Cancel")) {  display.setCurrent(listAddress);  }  }  }  }


Download ppt "모바일 자바 프로그래밍 MIDP RMS Ps lab 오민경. MIDP RMS  RMS (Record Management System)  MIDP 에서 정의하는 영속성을 지닌 자체 데이터 저장 공간  Record Store 의 집합으로 구성된 아주 간단한 데이터베이스."

Similar presentations


Ads by Google