3

如何使用 javadriver 检索 MongDB 数据并加载到文本框中?

我尝试了以下代码来显示数据,但我想在文本框中获取数据:

BasicDBObject doc = new BasicDBObject();
doc.put("Name", v2);
doc.put("SID", n4);
doc.put("University", v4);
DBCursor Cur = coll.find(doc);   
System.out.println(Cur);
4

1 回答 1

4

下面的代码片段展示了如何在迭代查询结果时提取文档的各个字段。如果需要,您可以获取每个字段并将其放入 GUI 的文本框中。

完整的代码示例在这里:https ://gist.github.com/3087822

private static void queryAndDisplayStudents(DBCollection students)
{
    // Get all students (no query criteria).
    DBCursor cursor = students.find();

    // Iterate over the students.
    while (cursor.hasNext()) 
    {
        // Display each student.

        DBObject student = cursor.next();

        // Get the individual fields of the student document.
        // These individual fields could, for example, 
        // be put in text fields of a GUI.
        String name = (String) student.get("Name");
        Number sid  = (Number) student.get("SID");
        String university = (String) student.get("University");

        // Given that we are not actually building a GUI, 
        // just display the fields on the command line.
        System.out.printf("Student name: %s, SID: %d, University: %s%n", 
                          name, sid, university);
    }        
}
于 2012-07-11T03:40:11.747 回答