db4o数据库的基本操作

db4o数据库的基本操作对db4o(面向对象的数据库)的基本操作: 数据库的链接:privatestaticObjectContainerdb; finalstaticStringDB4OFILENAME=System.getProperty("user.dir") +"/database.db4o"; static{ db=Db4oEmbedde…

大家好,又见面了,我是你们的朋友全栈君。

对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账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号