RMS Record Store static Record Store open Record
- Slides: 14
RMS 사용 ± Record. Store 객체 열기 ® static Record. Store open. Record. Store (String Record. Store. Name, boolean create. If. Necessary); ± Record. Store 객체 닫기 ® void close. Record. Store(); ± Record. Store 내의 레코드에 대한 인덱싱하기 ® Record. Enumeration 인터페이스를 이용하여 Record. Store내의 레코드들을 훑어 볼 수 있다. ® Record. Enumeration enumerate. Records(Record. Filter filter, Record. Comparator comparator, boolean keep. Updated);
RMS 사용 (cont. ) ± Record. Store에 레코드 추가 ® int add. Record(byte[] data, int offset, int num. Bytes); ± Record. Store내의 레코드 삭제 ® void delete. Record(int record. Id); ± Record. Store내의 레코드 열람하기 ® byte[] get. Record(int record. Id);
주소록 예제 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. import import java. io. *; java. util. *; javax. microedition. midlet. *; javax. microedition. lcdui. *; javax. microedition. rms. *; public class Addr. Book. MIDlet extends MIDlet implements Command. Listener { private Display display; private List list. Address; private Form form. Add; private Record. Store db; private Alert alert. View; private Vector idlist; public Addr. Book. MIDlet() { // 주소록 List 객체 생성 list. Address = new List("Address Book", List. IMPLICIT); list. Address. add. Command(new Command("View", Command. ITEM, 0)); list. Address. add. Command(new Command("Add", Command. SCREEN, 1)); list. Address. add. Command(new Command("Remove", Command. ITEM, 1));
주소록 예제 (cont. ) list. Address. add. Command(new Command("Exit", Command. EXIT, 1)); list. Address. set. Command. Listener(this); 22. 23. // 주소록 추가 Form 객체 생성 form. Add = new Form("Add Address"); form. Add. append(new Text. Field("Name", "", 10, Text. Field. ANY)); form. Add. append(new Text. Field("E-Mail", "", 32, Text. Field. EMAILADDR)); form. Add. append(new Text. Field("Address", "", 64, Text. Field. ANY)); form. Add. append(new Text. Field("Tel", "", 13, Text. Field. PHONENUMBER)); form. Add. add. Command(new Command("OK", Command. OK, 0)); form. Add. add. Command(new Command("Cancel", Command. CANCEL, 0)); form. Add. set. Command. Listener(this); 24. 25. 26. 27. 28. 29. 30. 31. 32. // 주소록 출력 Alert 객체 생성 alert. View = new Alert(""); alert. View. set. Timeout(Alert. FOREVER); 33. 34. 35. 36. } 37. public void start. App() { display = Display. get. Display(this); db = open. DB(); load. DB(db); display. set. Current(list. Address); } 38. 39. 40. 41. 42.
주소록 예제 (cont. ) 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. public void pause. App() { try { db. close. Record. Store(); } catch(Exception e) {} } public void destroy. App(boolean unconditional) { try { db. close. Record. Store(); } catch(Exception e) {} } public Record. Store open. DB() { Record. Store rs; try { rs = Record. Store. open. Record. Store("addrbook", true); } catch(Record. Store. Exception ex) { rs = null; } return rs; }
주소록 예제 (cont. ) 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. 78. 79. 80. 81. 82. 83. 84. /** * RMS에서 주소록 정보를 모두 읽어들입니다. */ public boolean load. DB(Record. Store rs) { // 리스트를 비운다. while(list. Address. size()>0) list. Address. delete(0); idlist = new Vector(); // 주소록 레코드 저장소 내의 레코드 나열을 얻습니다. Record. Enumeration enum = null; try { enum = rs. enumerate. Records(null, false); } catch(Record. Store. Not. Open. Exception ex) { return false; } // 레코드 나열에서 각 레코드의 ID를 읽어서 저장한다. while(enum. has. Next. Element()) { try { int id = enum. next. Record. Id(); idlist. add. Element(new Integer(id)); } catch(Exception ex) { } } enum. destroy();
주소록 예제 (cont. ) // 각 레코드를 읽어 리스트에 등록합니다. for (int i=0; i<idlist. size(); i++) { String name = null; try { int id = ((Integer)idlist. element. At(i)). int. Value(); byte[] record = rs. get. Record(id); Byte. Array. Input. Stream bais = new Byte. Array. Input. Stream(record); Data. Input. Stream dis = new Data. Input. Stream(bais); name = dis. read. UTF(); dis. close(); } catch(Exception ex) { name = null; } 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. if (name==null) idlist. remove. Element. At(i); else list. Address. append(name, null); 98. 99. 100. 101. } return true; 102. 103. 104. }
주소록 예제 (cont. ) 105. 106. 107. 108. 109. 110. 111. 112. 113. 114. 115. 116. 117. 118. 119. 120. 121. 122. 123. 124. 125. 126. 127. /** * 레코드 저장소에 새로운 주소를 추가합니다. */ public boolean add. Address(Record. Store rs, String name, String email, String address, String tel) { try { Byte. Array. Output. Stream baos = new Byte. Array. Output. Stream(); Data. Output. Stream dos = new Data. Output. Stream(baos); dos. write. UTF(name); dos. write. UTF(email); dos. write. UTF(address); dos. write. UTF(tel); byte[] b = baos. to. Byte. Array(); int id = rs. add. Record(b, 0, b. length); idlist. add. Element(new Integer(id)); list. Address. append(name, null); dos. close(); } catch(Exception ex) { return false; } return true; }
주소록 예제 (cont. ) 128. 129. 130. 131. 132. 133. 134. 135. 136. 137. 138. 139. 140. 141. 142. 143. 144. 145. 146. 147. 148. 149. 150. 151. /** * 레코드 저장소에서 주소를 삭제합니다. */ public boolean remove. Address(Record. Store rs, int index) { try { int id = ((Integer)idlist. element. At(index)). int. Value(); rs. delete. Record(id); idlist. remove. Element. At(index); list. Address. delete(index); } catch(Exception ex) { return false; } return true; } /** * 레코드 저장소에서 주소를 읽어옵니다. */ public String view. Address(Record. Store rs, int index) { String result = null; try { int id = ((Integer)idlist. element. At(index)). int. Value(); byte[] record = rs. get. Record(id);
주소록 예제 (cont. ) 153. Byte. Array. Input. Stream bais = new Byte. Array. Input. Stream(record); Data. Input. Stream dis = new Data. Input. Stream(bais); 154. String. Buffer strbuf = new String. Buffer(); 155. // 이름 읽어들이기 strbuf. append("NAME : n"); strbuf. append(dis. read. UTF()); strbuf. append("n"); 152. 156. 157. 158. 159. 160. 161. 162. 163. 164. 165. 166. 167. 168. 169. 170. // 메일주소 읽어들이기 strbuf. append("E-MAIL : n"); strbuf. append(dis. read. UTF()); strbuf. append("n"); // 주소 읽어들이기 strbuf. append("Address : n"); strbuf. append(dis. read. UTF()); strbuf. append("n"); // 전화번호 읽어들이기 strbuf. append("Tel : n"); strbuf. append(dis. read. UTF()); strbuf. append("n");
주소록 예제 (cont. ) 171. dis. close(); 172. result = strbuf. to. String(); } catch(Exception ex) { result = null; } return result; 173. 174. 175. 176. 177. } 178. public void command. Action(Command c, Displayable s) { if (s == list. Address) { // 현재 화면이 List이면 if (c == List. SELECT_COMMAND || c. get. Label(). equals("View")) { // 선택된 주소록 출력 String address = view. Address(db, list. Address. get. Selected. Index()); alert. View. set. String(address); display. set. Current(alert. View); } else if (c. get. Label(). equals("Add")) { // 폼 초기화 ((Text. Field)form. Add. get(0)). set. String(""); ((Text. Field)form. Add. get(1)). set. String(""); ((Text. Field)form. Add. get(2)). set. String(""); ((Text. Field)form. Add. get(3)). set. String(""); display. set. Current(form. Add); 179. 180. 181. 182. 183. 184. 185. 186. 187. 188. 189. 190. 191.
주소록 예제 (cont. ) } else if (c. get. Label(). equals("Remove")) { remove. Address(db, list. Address. get. Selected. Index()); } else if (c. get. Label(). equals("Exit")) { // 프로그램 종료 destroy. App(false); notify. Destroyed(); } } else if (s == form. Add) { // 현재 화면이 Form 이면 if (c. get. Label(). equals("OK")) { add. Address(db, ((Text. Field)form. Add. get(0)). get. String(), ((Text. Field)form. Add. get(1)). get. String(), ((Text. Field)form. Add. get(2)). get. String(), ((Text. Field)form. Add. get(3)). get. String()); display. set. Current(list. Address); } if (c. get. Label(). equals("Cancel")) { display. set. Current(list. Address); } } 192. 193. 194. 195. 196. 197. 198. 199. 200. 201. 202. 203. 204. 205. 206. 207. 208. 209. 210. 211. } 212. 213. }