pt.ua.cbd.cbd_project.database.MongoDatabaseConnector.java Source code

Java tutorial

Introduction

Here is the source code for pt.ua.cbd.cbd_project.database.MongoDatabaseConnector.java

Source

/*
 * 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 pt.ua.cbd.cbd_project.database;

import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoDatabase;
import org.bson.Document;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import static java.util.Arrays.asList;
import org.apache.log4j.Logger;

/**
 *
 * @author Maciek
 */
public class MongoDatabaseConnector {
    final static Logger logger = Logger.getLogger(Base.class);

    private String dbName = "bookDocumentDB";
    private MongoDatabase db;

    public MongoDatabaseConnector() {
        try {
            // To connect to mongodb server
            MongoClient mongoClient = new MongoClient();
            // Now connect to your databases
            this.db = mongoClient.getDatabase(this.dbName);
            logger.info("Connect to mongo database successfully");
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
    }

    public MongoDatabase connect() {
        return this.db;
    }

}