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 exifIndexer; import com.mongodb.DB; import com.mongodb.MongoClient; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; import java.net.UnknownHostException; import java.util.Arrays; import java.util.logging.Level; import java.util.logging.Logger; /** * * @author JOSE */ public class MongoHandler { private final String user = "SuperAwesomeUser"; private final char[] password = ("OlaKAse").toCharArray(); private final String host = "localhost"; private final Integer port = 27017; private final String bdname = "DBWork"; private MongoClient mongoClient; public MongoHandler() { } // Realiza la conexin con la bd de mongo public DB connect() { MongoCredential credential = MongoCredential.createMongoCRCredential(user, bdname, password); MongoClient Client; try { Client = new MongoClient(new ServerAddress(host, port), Arrays.asList(credential)); } catch (UnknownHostException ex) { System.err.println("Error creating Mongo DB Client"); } try { Client = new MongoClient(host, port); DB db = Client.getDB(bdname); //Elimino la bd por si hubiera datos mongoClient = Client; //db = deletedb(bdname); // mongoClient.getDB(bdname).dropDatabase(); return db; } catch (UnknownHostException ex) { Logger.getLogger(MongoHandler.class.getName()).log(Level.SEVERE, null, ex); return null; } } // vacia la bd de mongo public void deletedb(String namedb) { this.mongoClient.getDB(namedb).dropDatabase(); } public String getBdname() { return bdname; } public MongoClient getMongoClient() { return mongoClient; } public void setMongoClient(MongoClient mongoClient) { this.mongoClient = mongoClient; } }