我已經解決了它,所以在將來如果有人看這個問題,他們可以參考這個答案。
使用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());
}
'jlist'不是你的問題分配的Java。請嘗試顯示[mcve] –