上學的時候就聽老師說過有對象數據庫。
但是我所接觸的數據庫都是關系型數據庫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
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操作。
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的學習文檔。
http://www.ibm.com/developerworks/cn/java/jdb4o/?ca=j-h