Clase MongoClientProvider

Esta clase nos proporciona la conexión a una base de datos MongoDB, es recomendable establecer los controles de seguridad para acceso a la base de datos.

Si tienes configurado la autenticación de usuarios se debe quitar los comentarios del código e indicar los valores correspondientes.

Clase: MongoClientProvider

public class MongoClientProvider {
    private MongoClient mongoClient = null;

    public MongoClient getMongoClient() {
        mongoClient = new MongoClient();
        try {
            /**
             * autentificacion
             */
            /*
            String database = "";
            String username = "";
            String password = "";
            String host = "localhost";
            int port = 27017;
            char[] charArray = password.toCharArray();
            MongoCredential credential = MongoCredential.createCredential(username, database, charArray);
            mongoClient = new MongoClient(new ServerAddress(host, port), Arrays.asList(credential));
             */
        } catch (Exception e) {
            System.out.println("getMongoClient() " + e.getLocalizedMessage());
        }
        return mongoClient;
    }
}

Last updated