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 に答える