2012-07-10 27 views
3

如何檢索MongDB數據並使用javadriver在文本框中加載?如何通過javadriver檢索文本框中的MongoDB數據?

我嘗試下面的代碼來顯示數據,但我想在文本框的數據:

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

下面的代碼片段顯示瞭如何當您遍歷結果中提取一個文件的各個字段的查詢。如果你願意,你可以把每個領域放在一個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); 
    }   
} 
+0

謝謝伊恩工程:) – jad001 2012-07-15 01:04:30

+0

如果什麼文件嵌套像 '「名稱」:{「名」:「鮑勃」,」第二個名字「:」Lien「}, 」SID「:10,」University「:」INA「' 現在如何在文本框中顯示'Name'文檔(嵌套),上面給出的代碼是爲了簡單文件,現在我想顯示嵌套或嵌入式文件的數據。我希望你能理解我的問題。 – jad001 2012-07-25 18:52:36

+0

plz幫我嵌套一個?如果文檔嵌套,我們將如何訪問java中的嵌套文檔? – jad001 2012-07-27 20:38:55

相關問題