2017-04-21 53 views
-2

我想列出所有的數據庫到jlist,其工作正常只有我不能列入JLIST的問題。從mongodb添加值到jlist java

 private void dblist() throws UnknownHostException { 
      MongoClient mongo = new MongoClient("localhost", 27017); 
      java.util.List<String> dbs = mongo.getDatabaseNames(); 

      for (String a : dbs) { 
       System.out.println(a); // here i am already getting value 
             // just need to add into jlist 
       System.out.println(a); 

       jlist.add(a); // adding value into Jlist but its giving 
           // me error 
      } 
     } 
    } 

System.out.println(a) result

+0

'jlist'不是你的問題分配的Java。請嘗試顯示[mcve] –

回答

0

你可以試試這個將其轉換?;

String[] x = new String[dbs.size()]; 
JList<String> list = new JList<>(dbs.toArray(x)); 

更新:

你可以試試這個請;

yourJlist = new JList(dbs.toArray()); 

更新2:

此代碼工作你爲什麼不嘗試這樣;

MongoClient mongo = new MongoClient("localhost", 27017); 
MongoCursor<String> dbs = mongo.listDatabaseNames().iterator(); 
while(dbs.hasNext()) { 
    System.out.println(dbs.next()); 
} 
+0

仍然給我一個錯誤,java.lang.NullPointerException @MuratGüner –

+0

我已經添加了另一個代碼,可以檢查它嗎? –

+0

錯誤不再,但列表似乎是空白@MuratGüner –

0

我已經解決了它,所以在將來如果有人看這個問題,他們可以參考這個答案。

使用MongoDB的Java驅動程序3.4.2

的MongoDB到JList的從Java的JList

try{ 
    String host=hname.getText(); 
    String port=spport.getValue().toString(); 
    Integer pt = Integer.valueOf(port);       
     try (MongoClient mongoClient = new MongoClient(new 
     ServerAddress(host, pt),    
       MongoClientOptions.builder() 
         .serverSelectionTimeout(2000) 
         .build())) { 
      DB db = mongoClient.getDB(jlist.getSelectedValue().toString()); 
      DBbtn.setText("DB Connected"); 
      DBbtn.setBackground (new Color(109,184,0)); 
      DBbtn.setForeground(Color.WHITE); 
      Set<String> colls = db.getCollectionNames(); 

      DefaultListModel listModel = new DefaultListModel(); 
      colls.stream().map((s) -> { 
       listModel.addElement(s); 
       return s; 
      }).forEach((s) -> { 
       System.out.println(s); 
      }); 
      collectionss.setModel(listModel); // collectionss is jlist name 
     } 

    } catch (Exception e) { 
    } 

插入到MongoDB的

try{   
     String logid="Log-"+logname.getText(); 
     JList dataList=(loglist); 
     int sixe=dataList.getModel().getSize(); 
    ArrayList arrayList = new ArrayList(); 
    for (int i = 0; i <sixe; i++) { 
    arrayList.add(dataList.getModel().getElementAt(i)); 
    server.total.setText("Total Log-"+i); 

    } 
    System.out.println(arrayList); 
    Iterator itr = arrayList.iterator(); 
    String host=hname.getText(); 
    String port=spport.getValue().toString();      
    MongoClient mongoClient = new MongoClient(asList(new ServerAddress(host+":"+port)), 
            singletonList(MongoCredential.createCredential(uname.getText(), 
                        dbname.getText(), 
                        pass.getText().toCharArray())), 
            MongoClientOptions.builder().serverSelectionTimeout(2000).build()); 

     DB db = mongoClient.getDB(jlist.getSelectedValue().toString()); 
     DBCollection bookCollection = 
     db.getCollection(collectionss.getSelectedValue().toString()); 
     BasicDBObject doc = new BasicDBObject(logid, arrayList); 
     bookCollection.insert(doc); 
     server.consolelog.setText("INFO: Data is inserted succsesfully"); 

     }catch(Exception e){ 
      server.consolelog.setText(e.toString()); 
     }