大家好,又见面了,我是你们的朋友全栈君。
对db4o(面向对象的数据库)的基本操作:
数据库的链接:
private static ObjectContainer db;
final static String DB4OFILENAME = System.getProperty("user.dir")
+ "/database.db4o";
static{
db = Db4oEmbedded.openFile(
Db4oEmbedded.newConfiguration(), DB4OFILENAME);
}
数据库存储:
public void store(Object obj) {
ObjectContainer db = Db4oEmbedded.openFile(
Db4oEmbedded.newConfiguration(), DB4OFILENAME);
try {
db.store(obj);
} finally {
db.close();
}
}
数据的查询,更新,和删除操作:
public void queryObject() {
ObjectContainer db = Db4oEmbedded.openFile(
Db4oEmbedded.newConfiguration(), DB4OFILENAME);
List<Person> persons = null;
try{
persons = db.query(new Predicate<Person>() {
private static final long serialVersionUID = 1L;
public boolean match(Person person) {
// return "Allen".equals(person.getName());//根据特定的条件查询数据
return true;//查询数据库中对应表中所有的数据,此处为person表
}
});
for(Person per : persons) {
System.out.println(per.getName()+" : " + per.getPassword());
}
Person person = persons.get(0);
System.out.println(person);
person.setName("Person");
db.store(person);
// db.delete(person);
System.out.println("==================");
for(Person per : persons) {
System.out.println(per.getName()+" : " + per.getPassword());
}
} finally {
db.close();
}
}
完整的代码:
Person
public class Person {
private String id;
private String name;
private String password;
public Person(String id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Operation
import java.util.List;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.eight.study.bean.Person;
import com.db4o.query.Predicate;
import com.db4o.query.Query;
public class Operation {
// private static ObjectContainer db;
final static String DB4OFILENAME = System.getProperty("user.dir")
+ "/database.db4o";
// static{
// db = Db4oEmbedded.openFile(
// Db4oEmbedded.newConfiguration(), DB4OFILENAME);
// }
// public static ObjectContainer DB4oConnection() {
// return db;
// }
//db4o 数据库存储对象
public void store(Object obj) {
ObjectContainer db = Db4oEmbedded.openFile(
Db4oEmbedded.newConfiguration(), DB4OFILENAME);
try {
db.store(obj);
} finally {
db.close();
}
}
//db4o 数据库进行查询,第一种查询方法
public void queryObject() {
ObjectContainer db = Db4oEmbedded.openFile(
Db4oEmbedded.newConfiguration(), DB4OFILENAME);
List<Person> persons = null;
try{
persons = db.query(new Predicate<Person>() {
private static final long serialVersionUID = 1L;
public boolean match(Person person) {
// return "Allen".equals(person.getName());//根据特定的条件查询数据
return true;//查询数据库中对应表中所有的数据,此处为person表
}
});
for(Person per : persons) {
System.out.println(per.getName()+" : " + per.getPassword());
}
Person person = persons.get(0);
System.out.println(person);
person.setName("Person");
db.store(person);
// db.delete(person);
System.out.println("==================");
for(Person per : persons) {
System.out.println(per.getName()+" : " + per.getPassword());
}
} finally {
db.close();
}
}
//db4o 数据库进行查询,第二种查询方法
public void queryObject(String name) {
ObjectContainer db = Db4oEmbedded.openFile(
Db4oEmbedded.newConfiguration(), DB4OFILENAME);
List<Person> persons = null;
try{
Query query = db.query();
query.constrain(Person.class);
query.descend("name").constrain(name);
persons = query.execute();
System.out.println(persons.get(0).getName()+" : " + persons.get(0).getPassword());
} finally {
db.close();
}
}
//db4o 删除一个对象
public void deleteObject(Object obj) {
ObjectContainer db = Db4oEmbedded.openFile(
Db4oEmbedded.newConfiguration(), DB4OFILENAME);
try {
db.delete(obj);
} finally {
db.close();
}
}
public static void main(String[] args) {
System.out.println(DB4OFILENAME);
Operation op = new Operation();
// Person person = new Person("000001", "Allen", "123456");
// Person person1 = new Person("000002", "Jeson", "654321");
// Person person2 = new Person("000003", "Aimi", "234156");
// op.store(person);
// op.store(person1);
// op.store(person2);
op.queryObject();
// op.queryObject("Aimi");
// op.deleteObject(person2);
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/162573.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...