您的位置:首页 > 其它

J2ME中面向对象方式实现数据管理(RMS)

2009-04-22 13:48 876 查看
本例实现的功能:

1.MVC结构

2.数据存储

3.数据筛选

4.数据排序





相关代码如下:

/**
* @作者 Jcuckoo
* @创建日期 2009-4-21
* @版本 V 1.0
*/
public class MyFilter implements RecordFilter {
private String search=null;
public MyFilter(String search) {
this.search = search.toLowerCase();
}
public boolean matches(byte[] reference) {
String temp=new String(reference).toLowerCase();
if(temp!=null&&temp.indexOf(search)!=-1){
return true;
}else{
return false;
}
}
}


package chapter08;

import java.io.ByteArrayInputStream;
import java.io.DataInputStream;

import javax.microedition.rms.RecordComparator;

/**
* @作者 Jcuckoo
* @创建日期 2009-4-22
* @版本 V 1.0
* 按照学生年龄进行排序
*/
public class MyComparator implements RecordComparator {
private ByteArrayInputStream bais=null;
private DataInputStream dis=null;
public int compare(byte[] rec1, byte[] rec2) {
int recInt1, recInt2;
try {
bais = new ByteArrayInputStream(rec1);
dis = new DataInputStream(bais);
dis.readUTF();
recInt1 = dis.readInt();

bais = new ByteArrayInputStream(rec2);
dis = new DataInputStream(bais);
dis.readUTF();
recInt2 = dis.readInt();
System.out.println(recInt1+" "+recInt2);
if (recInt1 > recInt2) {
return RecordComparator.FOLLOWS;
} else if (recInt1 < recInt2) {
return RecordComparator.PRECEDES;
} else {
return RecordComparator.EQUIVALENT;
}
} catch (Exception e) {
return 3;
}
}

}


package chapter08;

/**
* @作者 Jcuckoo
* @创建日期 2009-4-9
* @版本 V 1.0
*/
public class Student {
private String name;
private int age;
private boolean sex;

public Student() {
}
public Student(String name,int age, boolean sex) {
this.age = age;
this.name = name;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
}


package chapter08;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;

import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;

/**
* @作者 Jcuckoo
* @创建日期 2009-4-9
* @版本 V 1.0
*/
public class StudentDao {
private RecordStore recordstore=null;
private String recordName="myRecordStore";
public StudentDao() {
openRecordStore(recordName);
}
public void openRecordStore(String recordName){
try {
recordstore=RecordStore.openRecordStore(recordName,true);
} catch (Exception e) {
System.out.println("创建记录存储器失败。");
e.printStackTrace();
}
}
public void closeRecordStore(){
try {
recordstore.closeRecordStore();
} catch (Exception e) {
System.out.println("关闭记录存储器失败。");
e.printStackTrace();
}
}
public void deleteRecordStore(){
if(RecordStore.listRecordStores()!=null){
try {
closeRecordStore();
RecordStore.deleteRecordStore(recordName);
} catch (Exception e) {
System.out.println("删除记录存储器失败。");
e.printStackTrace();
}
}
}
public boolean saveStudent(Student student){
byte[]object;
ByteArrayOutputStream bos=new ByteArrayOutputStream();
DataOutputStream dos=new DataOutputStream(bos);
try {
dos.writeUTF(student.getName());
dos.writeInt(student.getAge());
dos.writeBoolean(student.isSex());
System.out.println("写入数据成功");
dos.flush();
object=bos.toByteArray();
recordstore.addRecord(object, 0, object.length);
bos.reset();
bos.close();
dos.close();
return true;
} catch (Exception e) {
System.out.println("对象写入失败。");
e.printStackTrace();
return false;
}
}
public Student[] getAllStudent(){
byte[]object=new byte[1];
Student []students;
try {
ByteArrayInputStream bis=null;
DataInputStream dis=null;
students=new Student[recordstore.getNumRecords()];
for(int i=1;i<=recordstore.getNumRecords();i++){
if(recordstore.getRecordSize(i)>object.length){
object=new byte[recordstore.getRecordSize(i)];
}
recordstore.getRecord(i,object,0);
bis=new ByteArrayInputStream(object);
dis=new DataInputStream(bis);
//数组的下标是从0开始,而RMS是从1开始,故需要i-1
students[i-1]=new Student(dis.readUTF(),dis.readInt(),dis.readBoolean());
//重新定位“标识”在数据流中的位置
dis.reset();
}
bis.close();
dis.close();
return students;
} catch (Exception e) {
System.out.println("读取数据失败。");
return null;
}
}
public Student[] getStudentByFilter(){
byte[]object=new byte[(new Student()).toString().length()];
Student []students;
try {
MyFilter myFilter=new MyFilter("jcuckoo");
RecordEnumeration recEnumeration=recordstore.enumerateRecords(myFilter, null, false);
ByteArrayInputStream bis=null;
DataInputStream dis=null;
students=new Student[recEnumeration.numRecords()];
for(int i=0;i<recEnumeration.numRecords();i++){
int id = recEnumeration.nextRecordId();
if (recordstore.getRecordSize(id) > object.length) {
object = new byte[recordstore.getRecordSize(id)];
}
recordstore.getRecord(id, object, 0);
bis=new ByteArrayInputStream(object);
dis=new DataInputStream(bis);
students[i]=new Student(dis.readUTF(),dis.readInt(),dis.readBoolean());
//重新定位“标识”在数据流中的位置
dis.reset();
}
bis.close();
dis.close();
return students;
} catch (Exception e) {
System.out.println("error");
return null;
}
}

public Student[] getStudentByComparator(){
byte[]object=new byte[(new Student()).toString().length()];
Student []students;
try {
MyComparator myComparator=new MyComparator();
RecordEnumeration recEnumeration=recordstore.enumerateRecords(null, myComparator, false);
ByteArrayInputStream bis=null;
DataInputStream dis=null;
students=new Student[recEnumeration.numRecords()];
for(int i=0;i<recEnumeration.numRecords();i++){
int id = recEnumeration.nextRecordId();
if (recordstore.getRecordSize(id) > object.length) {
object = new byte[recordstore.getRecordSize(id)];
}
recordstore.getRecord(id, object, 0);
bis=new ByteArrayInputStream(object);
dis=new DataInputStream(bis);
students[i]=new Student(dis.readUTF(),dis.readInt(),dis.readBoolean());
//重新定位“标识”在数据流中的位置
dis.reset();
}
bis.close();
dis.close();
return students;
} catch (Exception e) {
System.out.println("error");
return null;
}
}
public Student[] getStudentByFilterComparator(){
byte[]object=new byte[(new Student()).toString().length()];
Student []students;
try {
MyFilter myFilter=new MyFilter("jcuckoo");
MyComparator myComparator=new MyComparator();
RecordEnumeration recEnumeration=recordstore.enumerateRecords(myFilter, myComparator, false);
ByteArrayInputStream bis=null;
DataInputStream dis=null;
students=new Student[recEnumeration.numRecords()];
for(int i=0;i<recEnumeration.numRecords();i++){
int id = recEnumeration.nextRecordId();
if (recordstore.getRecordSize(id) > object.length) {
object = new byte[recordstore.getRecordSize(id)];
}
recordstore.getRecord(id, object, 0);
bis=new ByteArrayInputStream(object);
dis=new DataInputStream(bis);
students[i]=new Student(dis.readUTF(),dis.readInt(),dis.readBoolean());
//重新定位“标识”在数据流中的位置
dis.reset();
}
bis.close();
dis.close();
return students;
} catch (Exception e) {
System.out.println("error");
return null;
}
}
}


package chapter08;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.ChoiceGroup;
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.Item;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
* @作者 Jcuckoo
* @创建日期 2009-4-9
* @版本 V 1.0
*/
public class ComplexOjbectRecord extends MIDlet implements CommandListener{
private Display display;
private Form mainForm;
private Alert alert;
private StringItem st;
private Command add;
private Command show;
private Command search;
private Command comparator;
private Command sea_comp;
private Command exit;
private StudentDao studao;
public ComplexOjbectRecord() {
display=Display.getDisplay(this);
//用到的按钮
exit=new Command("退出",Command.SCREEN,1);
add=new Command("添加学生信息",Command.SCREEN,1);
show=new Command("显示学生信息",Command.SCREEN,1);
search=new Command("选择显示学生信息",Command.SCREEN,1);
comparator=new Command("按年龄排序显示",Command.SCREEN,1);
sea_comp=new Command("筛选并排序",Command.SCREEN,1);
//主界面
mainForm=new Form("记录存储器演示");
mainForm.addCommand(add);
mainForm.addCommand(search);
mainForm.addCommand(comparator);
mainForm.addCommand(sea_comp);
mainForm.addCommand(show);
mainForm.addCommand(exit);
mainForm.setCommandListener(this);
studao=new StudentDao();
}
protected void destroyApp(boolean arg0) {
studao.deleteRecordStore();
}
protected void pauseApp() {
}
protected void startApp() throws MIDletStateChangeException {
display.setCurrent(mainForm);
}
public void commandAction(Command c, Displayable d) {
//退出
if(c==exit){
destroyApp(true);
notifyDestroyed();
}
//输入学生信息界面的add按钮
if(c==add){
studao.saveStudent(new Student("jcuckoo",30,true));
studao.saveStudent(new Student("Jerry",30,true));
studao.saveStudent(new Student("cuckoo",20,false));
studao.saveStudent(new Student("jCuckoo",23,true));
studao.saveStudent(new Student("Jcuckoo",32,true));
studao.saveStudent(new Student("JCuckoo",30,false));
alert=new Alert("提示信息","学生信息添加成功",null,AlertType.INFO);
alert.setTimeout(Alert.FOREVER);
display.setCurrent(alert);
}

//显示过滤后的学生信息
if(c==search){
showStudents(studao.getStudentByFilter());
}
//显示排序后学生信息
if(c==comparator){
showStudents(studao.getStudentByComparator());
}
//筛选并排序
if(c==sea_comp){
showStudents(studao.getStudentByFilterComparator());
}
//显示所有学生信息
if(c==show){
showStudents(studao.getAllStudent());
}
}
public void showStudents(Student[] students){
mainForm.deleteAll();

//如果返回的集合不为空,则循环显示。
st=new StringItem("姓名   年龄   性别","");
mainForm.append(st);
if(students!=null){
for(int i=0;i<students.length;i++){
Student student=students[i];
st=new StringItem(student.getName()+"   "+student.getAge()+"   "+student.isSex(),"");
mainForm.append(st);
}
}
else{
mainForm.deleteAll();
st=new StringItem("没有找到学生信息","");
mainForm.append(st);
}
}
}


 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐