Example usage for com.mongodb MongoClientURI MongoClientURI

List of usage examples for com.mongodb MongoClientURI MongoClientURI

Introduction

In this page you can find the example usage for com.mongodb MongoClientURI MongoClientURI.

Prototype

public MongoClientURI(final String uri) 

Source Link

Document

Creates a MongoURI from the given string.

Usage

From source file:br.com.ifspsaocarlos.gastock.library.Mongodb.java

public Mongodb() {

    try {//w  w  w . j  a  va  2 s .c  om

        mongoClient = new MongoClient(
                new MongoClientURI("mongodb://ifspsaocarlos:147258369@52.67.101.151/gastock"));
        db = mongoClient.getDB("gastock");

    } catch (Exception e) {
        System.out.println("Ocorreu um erro na conexo com o banco de dados");
    }

}

From source file:br.edu.ifpb.bdnc.start.write.factory.MongoConnectionFactory.java

public static MongoClient getConnection() {
    return new MongoClient(new MongoClientURI("mongodb://localhost:27017"));
}

From source file:br.ufg.inf.everemind.db.DatabaseConnection.java

License:Open Source License

private static MongoDatabase herokuConnection() throws UnknownHostException {
    MongoClientURI connectionString = new MongoClientURI("");
    MongoClient mongoClient = new MongoClient(connectionString);
    MongoDatabase database = mongoClient.getDatabase("");
    return database;
}

From source file:ch.qos.logback.contrib.mongodb.MongoDBAppenderBase.java

License:Open Source License

/**
 * If appender starts, create a new MongoDB connection and authenticate
 * user. A MongoDB database and collection in {@link #setUri(String)} is
 * mandatory, username and password are optional.
 *//* www. j ava 2  s .c o m*/
@Override
public void start() {
    try {
        if (uri == null) {
            addError("Please set a non-null MongoDB URI.");
            return;
        }
        MongoClientURI mongoURI = new MongoClientURI(uri);
        String database = mongoURI.getDatabase();
        String collection = mongoURI.getCollection();
        if (database == null || collection == null) {
            addError("Error connecting to MongoDB URI: " + uri + " must contain a database and a collection."
                    + " E.g. mongodb://localhost/database.collection");
            return;
        }
        mongoClient = mongoClientFactory.createMongoClient(mongoURI);

        DB db = this.mongoClient.getDB(database);
        eventsCollection = db.getCollection(collection);
        super.start();
    } catch (Exception exception) {
        addError("Error connecting to MongoDB URI: " + uri, exception);
    }
}

From source file:co.cask.hydrator.plugin.realtime.sink.MongoDBRealtimeSink.java

License:Apache License

@Override
public void initialize(RealtimeContext context) throws Exception {
    super.initialize(context);
    MongoClientURI clientURI = new MongoClientURI(config.connectionString);
    mongoClient = new MongoClient(clientURI);
    mongoDatabase = mongoClient.getDatabase(config.dbName);
}

From source file:com.aankam.servlet.MongoCrudServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from   w ww . j  a v  a 2  s.co  m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    MongoClientURI uri = new MongoClientURI("mongodb://Username:Pasword@ds061721.mongolab.com:61721/cs612");
    MongoClient client = new MongoClient(uri);
    DB db = client.getDB(uri.getDatabase());

    response.setContentType("text/html;charset=UTF-8");

    String serialNo = request.getParameter("serialNo");
    String name = request.getParameter("name");
    String emailId = request.getParameter("emailId");
    String operation = request.getParameter("operation");

    BasicDBObject customer;
    BasicDBObject updateQuery;
    BasicDBObject deleteQuery;
    BasicDBObject searchQuery;
    DBCollection customersCollection;
    customersCollection = db.getCollection("Customers");
    DBObject searchResult = null;
    boolean flag = false;
    DBCursor cursor;
    switch (operation) {
    case "create":
        customer = new BasicDBObject();
        customer.put("serialNo", serialNo);
        customer.put("name", name);
        customer.put("emailId", emailId);
        customersCollection.insert(customer);
        break;
    case "update":
        updateQuery = new BasicDBObject();
        updateQuery.append("serialNo", serialNo);
        cursor = customersCollection.find(updateQuery);
        customer = new BasicDBObject();
        customer.put("serialNo", serialNo);
        customer.put("name", name);
        customer.put("emailId", emailId);
        if (cursor.hasNext()) {
            customersCollection.update(updateQuery, customer);
            flag = true;
        }

        break;
    case "delete":
        deleteQuery = new BasicDBObject();
        deleteQuery.append("serialNo", serialNo);
        customersCollection.remove(deleteQuery);
        break;
    case "search":
        searchQuery = new BasicDBObject();
        searchQuery.append("serialNo", serialNo);
        cursor = customersCollection.find(searchQuery);
        while (cursor.hasNext()) {
            searchResult = cursor.next();
        }
        break;
    default:
        break;

    }
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet MongoCrudServlet</title>");
        out.println("</head>");
        out.println("<body>");

        switch (operation) {
        case "create":
            out.println("<h3>Customer was successfully created.</h3>");
            break;
        case "update":
            if (flag == true) {
                out.println("<h3>Customer was successfully updated.</h3>");
            } else {
                out.println("<h3>Customer was not found to update.</h3>");
            }

            break;
        case "delete":
            out.println("<h3>Customer was successfully deleted.</h3>");
            break;
        case "search":
            if (searchResult != null) {
                out.println("<h3>The following customer was found:</h3>");
                out.println("<h4>Serial No :" + searchResult.get("serialNo") + "</h4>");
                out.println("<h4>Name :" + searchResult.get("name") + "</h4>");
                out.println("<h4>Email Id :" + searchResult.get("emailId") + "</h4>");
            } else {
                out.println("<h3>Customer not found.</h3>");
            }
            break;
        default:
            break;
        }
        out.println("<a href=\"index.html\">Back to Main Form</a>");
        out.println("</body>");
        out.println("</html>");
    }
}

From source file:com.ai.tris.server.db.mongodb.TrisMongoClient.java

License:Apache License

/**
 * Create mongo database access client. If Client uri initializes failed or just is empty,
 * give up the creation.//www . j  ava  2s. co m
 *
 * @return mongo database client.
 */
private static MongoClient createMongoClient() {
    if (StringUtils.isEmpty(clientUri)) {
        throw new RuntimeException("Give up trying to create a client using empty uri.");
    }
    return new MongoClient(new MongoClientURI(clientUri));
}

From source file:com.baifendian.swordfish.common.job.struct.datasource.MongoDatasource.java

License:Apache License

@Override
public void isConnectable() throws Exception {
    MongoClient mongoClient = new MongoClient(new MongoClientURI(this.address));
    try {/*from  w w  w  .  j  a v a 2 s . co m*/
        MongoClientOptions options = MongoClientOptions.builder().connectTimeout(10).socketKeepAlive(false)
                .build();

        MongoDatabase db = mongoClient.getDatabase(this.database);
        for (Document doc : db.listCollections()) {
            logger.debug("{}", doc);
        }
    } finally {
        mongoClient.close();
    }
}

From source file:com.bilko.controller.BlogController.java

License:Apache License

private BlogController(final String uri) throws IOException {
    final MongoDatabase db = new MongoClient(new MongoClientURI(uri)).getDatabase("blog");

    config = createFreemarkerConfiguration();
    userDao = new UserDao(db);
    sessionDao = new SessionDao(db);
    blogPostDao = new BlogPostDao(db);

    port(8082);// ww w  . j a  va 2  s.co  m
    initRoutes();
}

From source file:com.bluedragon.mongo.MongoDSN.java

License:Open Source License

public synchronized void open() throws Exception {

    if (mongouri == null) {

        if (clientMongoMap.containsKey(ip + port)) {
            mdb = clientMongoMap.get(ip + port).open().getDatabase(db);
        } else {//from w  w w.  j  a  v  a 2 s.c o  m
            mongoclient = newClient(server + ":" + port, user, pass, db);
            MongoClientWrapper mcw = new MongoClientWrapper(mongoclient);
            clientMongoMap.put(ip + port, mcw);
            mdb = mcw.open().getDatabase(db);
        }

    } else {
        MongoClientURI clientURI = new MongoClientURI(mongouri);
        mongoclient = new MongoClient(clientURI);
        mdb = mongoclient.getDatabase(clientURI.getDatabase());
    }

    lastUsed = System.currentTimeMillis();
}