db4o - 检索对象时的奇怪字符

时间:2014-11-27 13:23:08

标签: java database db4o

import java.io.File;
import com.db4o.Db4o;
import com.db4o.Db4oEmbedded;
import com.db4o.ObjectContainer;
import com.db4o.ObjectSet;
import com.db4o.query.Query;

public class Student {

private String name;

public AlumnoBDOO(String name){
    this.name = name;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public static void main(String[] args) {
    ObjectContainer bd = Db4oEmbedded.openFile("students.db4o");
    try {

        Student s1 = new Student("Carl");
        bd.store(s1)
        showStudents(bd);

       } catch (Exception e) {
           e.printStackTrace();
       } finally {
           bd.close();
       }
    }

public static void showResult(ObjectSet rs){
    System.out.println("Retrieved "+rs.size()+" objects");
    while(rs.hasNext()){
        System.out.println(rs.next());
    }
}

public static void showStudents(ObjectContainer bd){
    Query query = bd.query();
    query.constrain(Student.class);
    query.descend("name");
    ObjectSet rs = query.execute();
    showResult(rs);
}

}

我只是想将一个Student存储在db4o数据库中,但当我想要检索所有这些时,它输出如下:

Student@61070a02

我使用Eclipse Juno和db40 v.8.0,我已将其添加为外部jar。 为什么我会得到那些奇怪的角色而不是" Carl"?

1 个答案:

答案 0 :(得分:1)

这并不奇怪,但是toString()方法的默认实现。要获得有意义的信息,您应该在Student类中覆盖此方法。