DBMNG数据库管理与应用

才能是来自独创性。独创性是思维、观察、理解和判断的一种独特的方式。
当前位置:首页 > 数据库基础 > DB4O

db4o入门实例

一、db4o简介:
    简单的说,就是可以用对象的方式存储数据,比如java的集合对象,javabean,pojo对象等。
    开源,可以基于dboGPL协议免费使用,最新版本目前到了6.1版本(java)。官方网站有基于MySql数据库和其他存取方式性能比较的数据,性能十分不错。传闻应用十分广泛,从金融系统到战斗机控制系统都有使用。

db4o 为我们带来的是这样一种面向对象的查询方式:
1.完全原生,目前查询语言能用Java和.NET表达 
2.100% 面向对象, 抛开对象-关系映射 
3.100% 的类型安全 查询语言能完全获取现代IDE的特性,
    比如语法检测、类型检测、重构,等等。

二、安装
只要将jar文件加到classpath中即可。

下载了之后,有PDF格式和HTML格式的指南文档。

三、入门实例:(运行以后将在当前目录中产生db.data数据库文件)import com.db4o.Db4o; import com.db4o.ObjectContainer; import com.db4o.ObjectSet; public class FirstStepsExample{ public static void main(String[] args) { ObjectContainer db=Db4o.openFile("db.data"); try { storeFirstPilot(db); storeSecondPilot(db); retrieveAllPilots(db); retrievePilotByName(db); retrievePilotByExactPoints(db); updatePilot(db); deleteFirstPilotByName(db); deleteSecondPilotByName(db); } finally { db.close(); } } public static void accessDb4o() { ObjectContainer db=Db4o.openFile("db.data"); try { // do something with db4o } finally { db.close(); } } public static void storeFirstPilot(ObjectContainer db) { Pilot pilot1=new Pilot("Michael Schumacher",100); db.set(pilot1); System.out.println("Stored "+pilot1); } public static void storeSecondPilot(ObjectContainer db) { Pilot pilot2=new Pilot("Rubens Barrichello",99); db.set(pilot2); System.out.println("Stored "+pilot2); } public static void retrieveAllPilotQBE(ObjectContainer db) { Pilot proto=new Pilot(null,0); ObjectSet result=db.get(proto); listResult(result); } public static void retrieveAllPilots(ObjectContainer db) { ObjectSet result=db.get(Pilot.class); listResult(result); } public static void retrievePilotByName(ObjectContainer db) { Pilot proto=new Pilot("Michael Schumacher",0); ObjectSet result=db.get(proto); listResult(result); } public static void retrievePilotByExactPoints(ObjectContainer db) { Pilot proto=new Pilot(null,100); ObjectSet result=db.get(proto); listResult(result); } public static void updatePilot(ObjectContainer db) { ObjectSet result=db.get(new Pilot("Michael Schumacher",0)); Pilot found=(Pilot)result.next(); found.addPoints(11); db.set(found); System.out.println("Added 11 points for "+found); retrieveAllPilots(db); } public static void deleteFirstPilotByName(ObjectContainer db) { ObjectSet result=db.get(new Pilot("Michael Schumacher",0)); Pilot found=(Pilot)result.next(); db.delete(found); System.out.println("Deleted "+found); retrieveAllPilots(db); } public static void deleteSecondPilotByName(ObjectContainer db) { ObjectSet result=db.get(new Pilot("Rubens Barrichello",0)); Pilot found=(Pilot)result.next(); db.delete(found); System.out.println("Deleted "+found); retrieveAllPilots(db); } public static void listResult(ObjectSet result) { System.out.println(result.size()); while(result.hasNext()) { System.out.println(result.next()); } } } public class Pilot { private String name; private int points; public Pilot(String name,int points) { this.name=name; this.points=points; } public int getPoints() { return points; } public void addPoints(int points) { this.points+=points; } public String getName() { return name; } public String toString() { return name+"/"+points; } }
运行结果: C:\Test>java FirstStepsExample
Stored Michael Schumacher/100
Stored Rubens Barrichello/99
2
Michael Schumacher/100
Rubens Barrichello/99
1
Michael Schumacher/100
1
Michael Schumacher/100
Added 11 points for Michael Schumacher/111
2
Michael Schumacher/111
Rubens Barrichello/99
Deleted Michael Schumacher/111
1
Rubens Barrichello/99
Deleted Rubens Barrichello/99
0
本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号