Java的面向对象数据库db4o
上学的时候就听老师说过有对象数据库。但是我所接触的数据库都是关系型数据库mysql,oracle,ms sql server,或是db2.
最近在ibm development work上看到一个名为db4o的对象数据。
才看第一章,学过Java的都应该很容易理解的。
确实它真的很简单。
看起来似乎就像是在一个Java的操作,而并非如我们所以为的那样的想关系型数据库中操作一样。
如果有兴趣的话,大家也可以去看看。
不过,文章作者也对db4o的一些缺点进行了列举。
自己并没有进行很深入的学习。
只是对其感兴趣罢了。
或许对系数据库可能让我们进入一个新的世界。
1
public
class
Person
2 {
3 public Person()
4 { }
5 public Person(String firstName, String lastName, int age)
6 {
7 this .firstName = firstName;
8 this .lastName = lastName;
9 this .age = age;
10 }
11
12 public String getFirstName() { return firstName; }
13 public void setFirstName(String value) { firstName = value; }
14
15 public String getLastName() { return lastName; }
16 public void setLastName(String value) { lastName = value; }
17
18 public int getAge() { return age; }
19 public void setAge( int value) { age = value; }
20
21 public String toString()
22 {
23 return
24 " [Person: " +
25 " firstName = " + firstName + " " +
26 " lastName = " + lastName + " " +
27 " age = " + age +
28 " ] " ;
29 }
30
31 public boolean equals(Object rhs)
32 {
33 if (rhs == this )
34 return true ;
35
36 if ( ! (rhs instanceof Person))
37 return false ;
38
39 Person other = (Person)rhs;
40 return ( this .firstName.equals(other.firstName) &&
41 this .lastName.equals(other.lastName) &&
42 this .age == other.age);
43 }
44
45 private String firstName;
46 private String lastName;
47 private int age;
48 }
49
数据库的insert
2 {
3 public Person()
4 { }
5 public Person(String firstName, String lastName, int age)
6 {
7 this .firstName = firstName;
8 this .lastName = lastName;
9 this .age = age;
10 }
11
12 public String getFirstName() { return firstName; }
13 public void setFirstName(String value) { firstName = value; }
14
15 public String getLastName() { return lastName; }
16 public void setLastName(String value) { lastName = value; }
17
18 public int getAge() { return age; }
19 public void setAge( int value) { age = value; }
20
21 public String toString()
22 {
23 return
24 " [Person: " +
25 " firstName = " + firstName + " " +
26 " lastName = " + lastName + " " +
27 " age = " + age +
28 " ] " ;
29 }
30
31 public boolean equals(Object rhs)
32 {
33 if (rhs == this )
34 return true ;
35
36 if ( ! (rhs instanceof Person))
37 return false ;
38
39 Person other = (Person)rhs;
40 return ( this .firstName.equals(other.firstName) &&
41 this .lastName.equals(other.lastName) &&
42 this .age == other.age);
43 }
44
45 private String firstName;
46 private String lastName;
47 private int age;
48 }
49
1
2 import com.tedneward.model. * ;
3
4 public class Hellodb4o
5 {
6 public static void main(String[] args)
7 throws Exception
8 {
9 ObjectContainer db = null ;
10 try
11 {
12 db = Db4o.openFile( " persons.data " );
13
14 Person brian = new Person( " Brian " , " Goetz " , 39 );
15
16 db.set(brian);
17 db.commit();
18 }
19 finally
20 {
21 if (db != null )
22 db.close();
23 }
24 }
25 }
26
或是用另外的一种方法进行insert操作。
2 import com.tedneward.model. * ;
3
4 public class Hellodb4o
5 {
6 public static void main(String[] args)
7 throws Exception
8 {
9 ObjectContainer db = null ;
10 try
11 {
12 db = Db4o.openFile( " persons.data " );
13
14 Person brian = new Person( " Brian " , " Goetz " , 39 );
15
16 db.set(brian);
17 db.commit();
18 }
19 finally
20 {
21 if (db != null )
22 db.close();
23 }
24 }
25 }
26
1
public
class
Hellodb4o
2 {
3 public static void main(String[] args)
4 throws Exception
5 {
6 ObjectContainer db = null ;
7 try
8 {
9 db = Db4o.openFile( " persons.data " );
10
11 Person brian = new Person( " Brian " , " Goetz " , 39 );
12 Person jason = new Person( " Jason " , " Hunter " , 35 );
13 Person clinton = new Person( " Brian " , " Sletten " , 38 );
14 Person david = new Person( " David " , " Geary " , 55 );
15 Person glenn = new Person( " Glenn " , " Vanderberg " , 40 );
16 Person neal = new Person( " Neal " , " Ford " , 39 );
17
18 db.set(brian);
19 db.set(jason);
20 db.set(clinton);
21 db.set(david);
22 db.set(glenn);
23 db.set(neal);
24
25 db.commit();
26
27 // Find all the Brians
28 ObjectSet brians = db.get( new Person( " Brian " , null , 0 ));
29 while (brians.hasNext())
30 System.out.println(brians.next());
31 }
32 finally
33 {
34 if (db != null )
35 db.close();
36 }
37 }
38 }
39
详细介绍请参看ibm的学习文档。
2 {
3 public static void main(String[] args)
4 throws Exception
5 {
6 ObjectContainer db = null ;
7 try
8 {
9 db = Db4o.openFile( " persons.data " );
10
11 Person brian = new Person( " Brian " , " Goetz " , 39 );
12 Person jason = new Person( " Jason " , " Hunter " , 35 );
13 Person clinton = new Person( " Brian " , " Sletten " , 38 );
14 Person david = new Person( " David " , " Geary " , 55 );
15 Person glenn = new Person( " Glenn " , " Vanderberg " , 40 );
16 Person neal = new Person( " Neal " , " Ford " , 39 );
17
18 db.set(brian);
19 db.set(jason);
20 db.set(clinton);
21 db.set(david);
22 db.set(glenn);
23 db.set(neal);
24
25 db.commit();
26
27 // Find all the Brians
28 ObjectSet brians = db.get( new Person( " Brian " , null , 0 ));
29 while (brians.hasNext())
30 System.out.println(brians.next());
31 }
32 finally
33 {
34 if (db != null )
35 db.close();
36 }
37 }
38 }
39
http://www.ibm.com/developerworks/cn/java/jdb4o/?ca=j-h