JAVA使用XML作为持久存储介质实现方法 - 编程入门网
xception ex){
System.out.println("Member:" + member.getId() + " add error!");
return false;
}
}
}
}
public boolean update(Member member) {
if (hasMember(member)) {
synchronized (lockObj) {
try{
memberPersistence.update(member);
Member oldMember = members.get(member.getId());
oldMember.setName(member.getName());
oldMember.setAge(member.getAge());
return true;
}
catch(XmlFileWriteException ex){
System.out.println("Member:" + member.getId() + " upate error!");
return false;
}
}
} else {
System.out.println("Member:" + member.getId()
+ " can’t been found!");
return false;
}
}
public boolean saveOrUpdate(Member member) {
if (hasMember(member)) {
return update(member);
} else {
return add(member);
}
}
public boolean delete(Member member) {
if (hasMember(member)) {
synchronized (lockObj) {
try{
memberPersistence.delete(member.getId());
members.remove(member.getId());
return true;
}catch(XmlFileWriteException ex){
System.out.println("Member:" + member.getId() + " delete error!");
return false;
}
}
} else {
System.out.println("Member:" + member.getId()
+ " can’t been found!");
return false;
}
}
@SuppressWarnings("unchecked")
public List<Member> search(MemberFilter memberFilter) {
ArrayList<Member> retval = new ArrayList<Member>();
for (Iterator it = members.keySet().iterator(); it.hasNext();) {
String key = (String) it.next();
Member member = members.get(key);
if (memberFilter.accept(member)) {
retval.add(member);
}
}
Collections.sort(retval);
return retval;
}
public List<Member> getAll() {
MemberFilter filter = new MemberFilter() {
public boolean accept(Member member) {
return true;
}
};
return search(filter);
}
public Member getMemberById(String id) {
for (Iterator it = members.keySet().iterator(); it.hasNext();) {
String key = (String) it.next();
Member member = members.get(key);
if (member.getId().equals(id)) {
return member;
}
}
return null;
}
}
JAVA使用XML作为持久存储介质实现方法(3)时间:2011-03-05MemberPersistence类是与XML文件打交道的类,通过它,数据才能真正存入持久介质-XML文件.它的函数都很好理解.这些函数工作时实际只会引发两种异常--读XML文件异常(一般由多个根节点导致)和写XML文件异常,会触发这些异常的 函数都应该对他们进行捕获和抛出处理.
|
凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢! |