我如何通过javadriver检索文本框中的MongoDB数据?

时间:2012-07-10 22:15:48

标签: mongodb textbox

如何使用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);

1 个答案:

答案 0 :(得分: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);
    }        
}