从 mongodb 向 jlist java 添加值
Adding value into jlist java from mongodb
我正在尝试将所有数据库列出到 jlist 中,它工作正常,只是我无法列出到 JLIST 中的问题。
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
}
}
}
你能试试这个转换吗?;
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());
}
我已经解决了所以以后如果有人找这个问题他们可以参考这个答案。
使用 mongodb java 驱动程序 3.4.2
mongodb 到 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) {
}
从 java jlist 插入到 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 中,它工作正常,只是我无法列出到 JLIST 中的问题。
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
}
}
}
你能试试这个转换吗?;
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());
}
我已经解决了所以以后如果有人找这个问题他们可以参考这个答案。
使用 mongodb java 驱动程序 3.4.2
mongodb 到 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) {
}
从 java jlist 插入到 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());
}