목차
1. 개요
2. 기본 이해사항
3. 구현 요소
4. 프로그램 동작원리
5. 실제구동
6. 참고서적
7. 소스코드 첨부
2. 기본 이해사항
3. 구현 요소
4. 프로그램 동작원리
5. 실제구동
6. 참고서적
7. 소스코드 첨부
본문내용
장되어 있음
id = _id;
init_log(rec);
}
public LogFile(long timer, String user, String description){ // 생성자
this.timer = timer;
this.user = user;
this.description = description;
}
public void init_log(byte[] rec){ // 레코드를 파싱해서 가져옴
ByteArrayInputStream bais = new ByteArrayInputStream(rec);
DataInputStream dis = new DataInputStream(bais);
try
{
timer = dis.readLong();
user = dis.readUTF();
description = dis.readUTF();
}catch(Exception e){}
}
public byte[] toBytes(){ // record store에 저장하기 위해 byte array로 변환한다.
byte data[] = null;
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeLong(timer);
dos.writeUTF(user);
dos.writeUTF(description);
data = baos.toByteArray();
baos.close();
dos.close();
}catch(Exception e){}
return data;
}
public String getTimeString(){ // 시간을 표현형태로 만들어줌
StringBuffer sb = new StringBuffer();
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(timer));
sb.append(cal.get(Calendar.MONTH)+1).append("/");
sb.append(cal.get(Calendar.DAY_OF_MONTH)).append("/");
sb.append(cal.get(Calendar.YEAR)).append(" ");
sb.append(cal.get(Calendar.HOUR_OF_DAY)).append(":");
if(cal.get(Calendar.MINUTE) < 10) sb.append(0);
sb.append(cal.get(Calendar.MINUTE));
return sb.toString();
}
public int getID(){return id;}
public void setID(int id){this.id = id;}
public long getTimer(){return timer;}
public void setTimer(long timer){this.timer = timer;}
public String getUser(){return user;}
public void setUser(String user){this.user = user;}
public String getDescription(){return description;}
public void setDescription(String description){this.description = description;}
}
/***************************************
* MAKEDB.JAVA *
***************************************/
import java.io.*;
import java.util.*;
import javax.microedition.rms.*;
public class makeDB{
RecordStore rs = null; // Mobile DB를 위한 RMI 생성
public makeDB(){
try
{
rs = RecordStore.openRecordStore("logfile", true); // DB파일을 연다.
}catch(Exception e){
System.out.println("Error : " + e.getMessage());
}
}
public void close(){
if(rs != null){
try
{
rs.closeRecordStore();
}catch(Exception e){}
}
}
public boolean save_log(LogFile lf){ // user이름과 설명을 저장한다.
if(rs == null) return false; // 만일 못열면 error
boolean success = false;
try
{
byte data[] = lf.toBytes(); // 바이트 단위로 바꾼다.( toBytes() 함수이용 )
int id = lf.getID();
if(id == 0){ // 새로운 레코드 생성
id = rs.addRecord(data, 0, data.length); // 레코드 추가
lf.setID(id);
}
success = true;
}catch(Exception e){
System.out.println("Error : " + e.getMessage());
}
return success;
}
public Vector retrieveAll(){ // 저장된 모든 log를 가져온다.(offline)
RecordEnumeration re = null;
Vector logs = new Vector();
try
{
re = rs.enumerateRecords(null,null,false);
while(re.hasNextElement()){ // 데이터가 더 있으면
int rec_id = re.nextRecordId(); // 다음 데이터 ID
// vector log에 현재 DB에 있는 데이터를 저장한다.
logs.addElement(new LogFile(rec_id, rs.getRecord(rec_id)));
}
}catch(Exception e){}
finally
{
if(re != null) re.destroy();
}
return logs;
}
}
id = _id;
init_log(rec);
}
public LogFile(long timer, String user, String description){ // 생성자
this.timer = timer;
this.user = user;
this.description = description;
}
public void init_log(byte[] rec){ // 레코드를 파싱해서 가져옴
ByteArrayInputStream bais = new ByteArrayInputStream(rec);
DataInputStream dis = new DataInputStream(bais);
try
{
timer = dis.readLong();
user = dis.readUTF();
description = dis.readUTF();
}catch(Exception e){}
}
public byte[] toBytes(){ // record store에 저장하기 위해 byte array로 변환한다.
byte data[] = null;
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeLong(timer);
dos.writeUTF(user);
dos.writeUTF(description);
data = baos.toByteArray();
baos.close();
dos.close();
}catch(Exception e){}
return data;
}
public String getTimeString(){ // 시간을 표현형태로 만들어줌
StringBuffer sb = new StringBuffer();
Calendar cal = Calendar.getInstance();
cal.setTime(new Date(timer));
sb.append(cal.get(Calendar.MONTH)+1).append("/");
sb.append(cal.get(Calendar.DAY_OF_MONTH)).append("/");
sb.append(cal.get(Calendar.YEAR)).append(" ");
sb.append(cal.get(Calendar.HOUR_OF_DAY)).append(":");
if(cal.get(Calendar.MINUTE) < 10) sb.append(0);
sb.append(cal.get(Calendar.MINUTE));
return sb.toString();
}
public int getID(){return id;}
public void setID(int id){this.id = id;}
public long getTimer(){return timer;}
public void setTimer(long timer){this.timer = timer;}
public String getUser(){return user;}
public void setUser(String user){this.user = user;}
public String getDescription(){return description;}
public void setDescription(String description){this.description = description;}
}
/***************************************
* MAKEDB.JAVA *
***************************************/
import java.io.*;
import java.util.*;
import javax.microedition.rms.*;
public class makeDB{
RecordStore rs = null; // Mobile DB를 위한 RMI 생성
public makeDB(){
try
{
rs = RecordStore.openRecordStore("logfile", true); // DB파일을 연다.
}catch(Exception e){
System.out.println("Error : " + e.getMessage());
}
}
public void close(){
if(rs != null){
try
{
rs.closeRecordStore();
}catch(Exception e){}
}
}
public boolean save_log(LogFile lf){ // user이름과 설명을 저장한다.
if(rs == null) return false; // 만일 못열면 error
boolean success = false;
try
{
byte data[] = lf.toBytes(); // 바이트 단위로 바꾼다.( toBytes() 함수이용 )
int id = lf.getID();
if(id == 0){ // 새로운 레코드 생성
id = rs.addRecord(data, 0, data.length); // 레코드 추가
lf.setID(id);
}
success = true;
}catch(Exception e){
System.out.println("Error : " + e.getMessage());
}
return success;
}
public Vector retrieveAll(){ // 저장된 모든 log를 가져온다.(offline)
RecordEnumeration re = null;
Vector logs = new Vector();
try
{
re = rs.enumerateRecords(null,null,false);
while(re.hasNextElement()){ // 데이터가 더 있으면
int rec_id = re.nextRecordId(); // 다음 데이터 ID
// vector log에 현재 DB에 있는 데이터를 저장한다.
logs.addElement(new LogFile(rec_id, rs.getRecord(rec_id)));
}
}catch(Exception e){}
finally
{
if(re != null) re.destroy();
}
return logs;
}
}