📄 model.java
字号:
/*
* Model.java
*
* Created on 2006年3月17日, 上午9:33
*
* To change this template, choose Tools | Options and locate the template under
* the Source Creation and Management node. Right-click the template and choose
* Open. You can then make changes to the template in the Source Editor.
*/
package com.j2medev.sample.phonebook;
import java.util.*;
import javax.microedition.rms.*;
import java.io.*;
/**
*
* @author Admin
*/
public class Model {
private RecordStore rs = null;
private RecordStore rs_index = null;
private RecordStore accout = null;
// private Vector entries = new Vector();
//添加联系人
public Model(){
try{
rs = RecordStore.openRecordStore("data", true);
rs_index = RecordStore.openRecordStore("dataindex", true);
accout = RecordStore.openRecordStore("accout",true);
}catch(RecordStoreException e){
e.printStackTrace();
}
}
private static class indexFilter implements RecordFilter{
private String key;
private int flag;
public indexFilter(String key,int flag){
this.key = key;
this.flag = flag;
}
public boolean matches(byte[] data){
if(key != null){
try{
return Index.matches(data,key,flag);
}catch(IOException e){
e.printStackTrace();
return false;
}
}else
return true;
}
}
public void addIndex(Index index) throws IOException{
try{
byte[] data = index.serialize();
if(rs_index.getNumRecords()>=0){
RecordEnumeration record = rs_index.enumerateRecords(new indexFilter(index.getKey(),0),null,false);
if(record.hasNextElement()){
rs_index.setRecord(record.nextRecordId(), data, 0, data.length);
}else{
//System.out.println("执行到这儿了");
rs_index.addRecord(data, 0, data.length);
}
}
}catch(RecordStoreException e){
e.printStackTrace();
}
}
public void addEntry(Entry entry){
try{
byte[] data = entry.serialize();
// System.out.println("执行到这儿了");
int id = rs.addRecord(data, 0, data.length);
Index index = new Index(entry.getName(), id);
addIndex(index);
}catch(IOException ex){
ex.printStackTrace();
}catch(RecordStoreException e){
e.printStackTrace();
}
//entries.addElement(entry);
}
//得到联系人信息
public Entry getEntry(String name){
try{
RecordEnumeration rei = rs_index.enumerateRecords(new indexFilter(name, 0), null, false);
Index index = null;
if(rei.numRecords() == 1){
int id = rei.nextRecordId();
index = Index.deserialize(rs_index.getRecord(id));
//int recordid = index.getRecordID();
byte[] data = rs.getRecord(index.getRecordID());
Entry entry = null;
return entry = Entry.deserialize(data);
}
}catch(RecordStoreException e){
e.printStackTrace();
}catch(IOException ex){
ex.printStackTrace();
return null;
}
return null;
// return (Entry)entries.elementAt(index);
}
/*/更新联系人信息
public void updateEntry(Entry entry){
for(int i =0;i<entries.size();i++){
Entry e = (Entry)entries.elementAt(i);
if(e.getId()==entry.getId()){
e.setName(entry.getName());
e.setMobile(entry.getMobile());
e.setPhone(entry.getPhone());
e.setEmail(entry.getEmail());
e.setAddress(entry.getAddress());
e.setQQ(entry.getQQ());
}
}
}
*/
//得到所有联系人
public Index[] getAll(String name,int flag) throws RecordStoreException,IOException{
RecordEnumeration re = rs_index.enumerateRecords(new indexFilter(name, flag), null, true);
byte[] data = new byte[20];
Index[] index = new Index[re.numRecords()];
int id =0;
int i = 0;
int length = -1;
while(re.hasNextElement()){
id = re.nextRecordId();
length = rs_index.getRecordSize(id);
if(length>data.length){
data = new byte[length + 10];
}
rs_index.getRecord(id, data, 0);
index[i] = Index.deserialize(data);
i++;
}
return index;
// Entry[] ret= new Entry[entries.size()];
// entries.copyInto(ret);
// return ret;
}
//删除联系人
public void deletRecord(String name) throws RecordStoreException,IOException{
int id = getanddelelindex(name, 0);
rs.deleteRecord(id);
/*
for(int i = 0; i<entries.size(); i++){
Entry e = (Entry)entries.elementAt(i);
if(e.getId() == entry.getId()){
entries.removeElementAt(i);
return;
}
}
**/
}
public int getanddelelindex(String key,int flag) throws RecordStoreException,IOException{
RecordEnumeration rei = rs_index.enumerateRecords(new indexFilter(key, flag), null, false);
Index index = null;
if(rei.numRecords() == 1){
int id = rei.nextRecordId();
index = Index.deserialize(rs_index.getRecord(id));
rs_index.deleteRecord(id);
}
return index.getRecordID();
}
// public int size(){
// return entries.size();
// }
/** Creates a new instance of Model */
// public Model() {
//}
//删除所有的record****************************
public void deletAllRecord() throws RecordStoreException
{
try
{
rs.closeRecordStore();
rs_index.closeRecordStore();
accout.closeRecordStore();
RecordStore.deleteRecordStore("data");
RecordStore.deleteRecordStore("dataindex");
RecordStore.deleteRecordStore("accout");
this.reOpenRecordStore();
}
catch (RecordStoreException e)
{
e.printStackTrace();
}
}
//用于删除所有记录后重新打开数据库。
private void reOpenRecordStore() throws RecordStoreException{
rs = RecordStore.openRecordStore("data",true);
rs_index = RecordStore.openRecordStore("dataindex",true);
accout = RecordStore.openRecordStore("accout",true);
}
//*************************************************
public boolean getinfor(){
try{
RecordEnumeration act = accout.enumerateRecords(null, null, false);
Accout _accout = null;
// if(act.numRecords() == 0){
// return true;
// }else
//System.out.println(act.numRecords());
if(act.numRecords() == 1){
int id = act.nextRecordId();
// System.out.println(id);
_accout = Accout.deserialize(accout.getRecord(id));
//int recordid = index.getRecordID();
return _accout.isAutologin();
}else
{
return true;
}
}catch(RecordStoreException e){
e.printStackTrace();
return true;
}
catch(IOException ex){
ex.printStackTrace();
return true;
}
// return true;
}
public boolean userandpass(String user,String pass){
try{
RecordEnumeration re1 = accout.enumerateRecords(null, null, false);
Accout _accout = null;
//= if(re1.numRecords() == 0){
// return true;用于设置初始密码111111
// }
//System.out.println(pass);
if(re1.numRecords() == 1){
int id = re1.nextRecordId();
_accout = Accout.deserialize(accout.getRecord(id));
//System.out.println(_accout.getUser()+_accout.getPass());
//int recordid = index.getRecordID();
if(_accout.getUser().equals(user) && _accout.getPass().equals(pass)){
return true;
}else
{return false;}
}else{return false;}
}catch(RecordStoreException e){
e.printStackTrace();
return false;
}catch(IOException ex){
ex.printStackTrace();
return false;
}
}
public boolean userandpass1(String user,String pass) throws IOException{
try{
RecordEnumeration re1 = accout.enumerateRecords(null, null, false);
Accout _accout = null;
if(re1.numRecords() == 0){
return true; ///用于设置初始密码111111
}
//
if(re1.numRecords() == 1){
int id = re1.nextRecordId();
_accout = Accout.deserialize(accout.getRecord(id));
//System.out.println(_accout.getUser()+_accout.getPass());
//int recordid = index.getRecordID();
if(_accout.getUser().equals(user) && _accout.getPass().equals(pass)){
return true;
}else
{return false;}
}else{return false;}
}catch(RecordStoreException e){
e.printStackTrace();
return false;
// }catch(IOException ex){
// ex.printStackTrace();
// return false;
}
}
public void saveloginset(String user,String pass,String flag) throws IOException{
boolean fag;
try{
accout.closeRecordStore();
RecordStore.deleteRecordStore("accout");
accout = RecordStore.openRecordStore("accout",true);
if(flag == "1"){
fag = true;
}else{
fag = false;
}
Accout act = new Accout(user, pass, fag);
// System.out.println(user+pass+flag);
byte[] data = act.serialize();
accout.addRecord(data, 0, data.length);
}catch(RecordStoreException e){
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -