Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package spntoolsdata.conn; import com.mongodb.DB; import com.mongodb.DBCollection; import com.mongodb.MongoClient; import java.net.UnknownHostException; import java.util.List; /** * * @author Ivansito */ public class connection { public boolean NewConnection(String IP) { MongoClient mongo = null; boolean exito = false; try { mongo = new MongoClient(IP, 27017); exito = ValidateMongoClient(mongo); } catch (UnknownHostException e) { } return exito; } public MongoClient Connection(String IP) { MongoClient mongo = null; try { mongo = new MongoClient(IP, 27017); } catch (UnknownHostException e) { } return mongo; } public DBCollection getCollectionUsers(String IP) { MongoClient mongo = Connection(IP); DB db = mongo.getDB("dbservispntools"); DBCollection DBUsers = db.getCollection("Usuarios"); return DBUsers; } public DBCollection getCollectionClients(String IP) { MongoClient mongo = Connection(IP); DB db = mongo.getDB("dbservispntools"); DBCollection DBClients = db.getCollection("Clientes"); return DBClients; } public boolean ValidateMongoClient(MongoClient mongo) { boolean exito = false; DB dbs = mongo.getDB("admin"); char pwd[] = { 'p', 'a', 's', 's', 'w', 'o', 'r', 'd' }; exito = dbs.authenticate("siteUserAdmin", pwd); if (mongo != null && exito) { //System.out.println("Data Base's: "); //PrintDataBase(mongo); exito = true; } else { System.out.println("Error: Conexin"); } return exito; } private void PrintDataBase(MongoClient mongo) { List dbs = mongo.getDatabaseNames(); for (int i = 0; i < dbs.size(); i++) { System.out.println(" - " + dbs.get(i).toString()); } } }