使用db4o的NQ进行数据库的查询,所使用的Person的对象已经在前面的文章中写过
import java.util.List;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.eight.study.bean.Person;
import com.db4o.query.Predicate;
public class DB4oQueryUseNQ {
final static String DB4OFILENAME = System.getProperty("user.dir")
+ "/database.db4o";
public static void NQQueryByCondition() {
ObjectContainer db = Db4oEmbedded.openFile(
Db4oEmbedded.newConfiguration(), DB4OFILENAME);
try {
List<Person> result = db.query(new Predicate<Person>() {
private static final long serialVersionUID = 1L;
public boolean match(Person person) {
return "Person".equals(person.getName());
}
});
for (Person per : result) {
System.out.println("id = " + per.getId()+
", name = "+ per.getName() + ", password = " + per.getPassword());
}
} finally {
db.close();
}
}
public static void NQQuery() {
ObjectContainer db = Db4oEmbedded.openFile(
Db4oEmbedded.newConfiguration(), DB4OFILENAME);
try {
List<Person> result = db.query(new Predicate<Person>() {
private static final long serialVersionUID = 1L;
public boolean match(Person person) {
return true;
}
});
for (Person per : result) {
System.out.println("id = " + per.getId()+
", name = "+ per.getName() + ", password = " + per.getPassword());
}
} finally {
db.close();
}
}
public static void NQQueryByManyCondition() {
ObjectContainer db = Db4oEmbedded.openFile(
Db4oEmbedded.newConfiguration(), DB4OFILENAME);
try {
List<Person> result = db.query(new Predicate<Person>() {
private static final long serialVersionUID = 1L;
public boolean match(Person person) {
return person.getId().equals("000003")
&&person.getName().equals("Aimi")
||person.getPassword().equals("132345");
}
});
for (Person per : result) {
System.out.println("id = " + per.getId()+
", name = "+ per.getName() + ", password = " + per.getPassword());
}
} finally {
db.close();
}
}
public static void main(String[] args) {
// DB4oQueryUseNQ.NQQueryByCondition();
// DB4oQueryUseNQ.NQQuery();
DB4oQueryUseNQ.NQQueryByManyCondition();
}
}