mongodb - Create a new instance of DB from a GUI - java -
i have implement simple gui connect mongodb client using java driver.
this picture of gui:
as can see, 1 can choose database , press ok connect.
in mongodbclient
class there's method (getdatabasesname
) detect databases names present:
import java.util.arraylist; import java.util.iterator; import java.util.list; import com.mongodb.mongoclient; public class mongodbclient { private mongoclient client; public mongodbclient() { try { this.client = new mongoclient("127.0.0.1", 27017); } catch (exception e) { // todo auto-generated catch block e.printstacktrace(); } } public mongoclient getclient() { return client; } public arraylist<string> getdatabasesname(){ arraylist<string> dbsname = new arraylist<>(); list<string> dbs = this.client.getdatabasenames(); iterator<string> iterator = dbs.iterator(); while(iterator.hasnext()){ dbsname.add(iterator.next()); } return dbsname; } }
then, in main
create new instance of mongodbclient , pass arraylist of databases names window1
class:
import java.util.arraylist; public class main { public static void main(string[] args) { // todo auto-generated method stub mongodbclient mongoclient = new mongodbclient(); arraylist<string> dbname = mongoclient.getdatabasesname(); try { window1 w1 = new window1(dbname); } catch (exception e) { system.out.println(e.getmessage()); } } }
finally, in window 1 can choose database group of buttons:
public void choosedb(arraylist<string> dbname) { dbbutton= new jradiobutton[dbname.size()]; buttongroup group = new buttongroup(); int x = 5; for(int = 0; i<dbname.size(); i++) { dbbutton[i] = new jradiobutton(dbname.get(i)); dbbutton[i].setbounds(x, 28, 65, 15); dbbutton[i].setbackground(color.white); this.panel.add(dbbutton[i]); x=x+100; group.add(dbbutton[i]); } }
after 1 presses ok, want create new instance of db, in main class not inside window1
class.
Comments
Post a Comment