Example usage for com.mongodb MongoClient getDatabase

List of usage examples for com.mongodb MongoClient getDatabase

Introduction

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

Prototype

public MongoDatabase getDatabase(final String databaseName) 

Source Link

Usage

From source file:MenuDefPred.java

public static void getPreds() {

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("database");
    MongoCollection<Document> elexirCollection = db.getCollection("test");

    FindIterable<Document> results = elexirCollection
            .find(new BasicDBObject("Types", new BasicDBObject("$gt", "0")));
    //FindIterable<Document> iter = elexirCollection.find(new BasicDBObject("derivProb", 2));

    for (Document doc : results) {
        List<String> conv = (List<String>) doc.get("Predicates");

        String[] convArr = new String[conv.size()];
        convArr = conv.toArray(convArr);

        for (String s : convArr)
            PredTextArea.append(s + "\n");
    }//  w w w . java2 s .co  m
}

From source file:Welcome.java

public void setListVal() {
    int i = 1;//from ww  w.  j  a  va 2 s  . c o  m
    MongoClient client = new MongoClient("localhost", 27017);
    MongoDatabase db = client.getDatabase("ExpenseManager");
    MongoCollection<Document> reg = db.getCollection("Registration");
    FindIterable<Document> find = reg.find(new Document("unm", unm));

    Document doc = find.first();

    DefaultListModel model;
    model = new DefaultListModel();
    while (i <= 5) {
        if (!doc.containsKey("Account" + i)) {
            i++;
        } else {
            while (doc.containsKey("Account" + i)) {
                String s = (String) doc.get("Account" + i);
                s = s.concat("              ");
                s = s.concat((String) doc.get("Bank" + i));
                s = s.concat("              ");
                s = s.concat((String) doc.get("Money" + i));
                model.addElement(s);
                i++;
            }
        }
    }
    jList1.setModel(model);
}

From source file:Welcome.java

private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
    /*MongoClient client = new MongoClient("localhost", 27017);
        DB db;//from  ww w  .  j  av  a2  s  .c o m
        db = client.getDB("ExpenseManager");
        DBCollection reg = (DBCollection) db.getCollection("Registration");
        DBObject query = new BasicDBObject("unm", unm);
        DBObject update = new BasicDBObject();
                
        update.put("$unset", new BasicDBObject("userId",""));
    */
    int i = 1;
    int count = 1;
    MongoClient client = new MongoClient("localhost", 27017);
    MongoDatabase db = client.getDatabase("ExpenseManager");
    MongoCollection<Document> reg = db.getCollection("Registration");
    FindIterable<Document> find = reg.find(new Document("unm", unm));
    DBObject query = new BasicDBObject("unm", unm);
    BasicDBObject update = new BasicDBObject();
    Map<Integer, Integer> m = new HashMap<Integer, Integer>();
    String val[] = new String[6];
    val[0] = "";
    Document doc = find.first();
    while (i <= 5) {
        if (!doc.containsKey("Account" + i)) {
            i++;
        } else {
            while (doc.containsKey("Account" + i)) {
                m.put(count, i);
                val[count] = (String) (doc.get("Account" + i));
                i++;
                count++;
            }
        }
    }
    JComboBox jcb = new JComboBox(val);
    jcb.setEditable(true);
    int msg = JOptionPane.showConfirmDialog(this, "Do You Really Want to remove Your Account?");
    if (msg == 0) {
        JOptionPane.showMessageDialog(this, jcb, "Select Your Account to Remove", JOptionPane.QUESTION_MESSAGE);

        while (jcb.getSelectedItem().equals("")) {
            JOptionPane.showMessageDialog(this, "You Haven't chose any Account, Please Choose the Account");
            JOptionPane.showMessageDialog(this, jcb, "Select Your Account to Remove",
                    JOptionPane.QUESTION_MESSAGE);
        }
        int index = jcb.getSelectedIndex();
        int value = m.get(index);
        String str1 = "Account" + value;
        String str2 = "Bank" + value;
        String str3 = "Money" + value;
        update.put("$unset", new BasicDBObject(str1, "").append(str2, "").append(str3, ""));
        UpdateResult updateOne = reg.updateOne(new Document("unm", unm), update);
        setListVal();

    }

    // TODO add your handling code here:
}

From source file:MenuDefCateg.java

public static void getCategories() {

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("database");
    MongoCollection<Document> elexirCollection = db.getCollection("test");

    FindIterable<Document> results = elexirCollection
            .find(new BasicDBObject("Types", new BasicDBObject("$gt", "0")));
    //FindIterable<Document> iter = elexirCollection.find(new BasicDBObject("derivProb", 2));

    for (Document doc : results) {
        List<String> conv = (List<String>) doc.get("Category");

        String[] convArr = new String[conv.size()];
        convArr = conv.toArray(convArr);

        for (String s : convArr)
            CategTextArea.append(s + "\n");

    }/*from w  w w  . ja v a  2 s  .c om*/
}

From source file:MenuIS.java

public static void getIS() {
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    MongoDatabase db = mongoClient.getDatabase("database");
    MongoCollection<Document> elexirCollection = db.getCollection("test");

    FindIterable<Document> results = elexirCollection
            .find(new BasicDBObject("derivProb", new BasicDBObject("$gt", "0")));

    //FindIterable<Document> iter = elexirCollection.find(new BasicDBObject("derivProb", 2));

    for (Document doc : results.limit(1)) {

        List<String> conv = (List<String>) doc.get("initialState");

        {/*from www.j  a  v  a  2  s.  com*/

            for (String s : conv)
                ISTextArea.append(s + "\n");

        }
    }
}

From source file:Register.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed

    // TODO add your handling code here:
    MongoClient client = new MongoClient("localhost", 27017);
    MongoDatabase db = client.getDatabase("ExpenseManager");
    MongoCollection<Document> reg = db.getCollection("Registration");
    String unm = jTextField2.getText();
    BasicDBObject query = new BasicDBObject("unm", unm);
    String pass = String.valueOf(jPasswordField1.getPassword());
    String c_pass = String.valueOf(jPasswordField2.getPassword());

    if (unm.length() == 0) {
        JOptionPane.showMessageDialog(null, "User Name is empty");
        this.setVisible(true);
    }/*from  w  ww.  jav  a2s. c o  m*/

    else if (reg.find(query).iterator().hasNext()) {
        JOptionPane.showMessageDialog(null, "User Name already exist");
        this.setVisible(true);
    } else if (jPasswordField1.getPassword().length == 0 || jPasswordField2.getPassword().length == 0) {
        JOptionPane.showMessageDialog(null, "Password Field Can not be empty");
        this.setVisible(true);
    } else if (!pass.equals(c_pass)) {
        JOptionPane.showMessageDialog(null, "Passwords do not match. Please Re-enter the password");
        this.setVisible(true);
    } else {
        String name = jTextField1.getText();
        Document ins = new Document("name", name).append("unm", unm).append("pass", pass);
        reg.insertOne(ins);
        System.out.println("Document is inserted into MongoDb");
        System.out.println(pass);
        db.createCollection(unm);
        this.dispose();
        login lgn = new login();
        lgn.setVisible(true);

    }

}

From source file:alxpez.blog.BlogController.java

License:Apache License

public BlogController(String mongoURIString) throws IOException {
    final MongoClient mongoClient = new MongoClient(new MongoClientURI(mongoURIString));
    final MongoDatabase blogDatabase = mongoClient.getDatabase("blog");

    blogPostDAO = new BlogPostDAO(blogDatabase);
    userDAO = new UserDAO(blogDatabase);
    sessionDAO = new SessionDAO(blogDatabase);

    cfg = createFreemarkerConfiguration();
    setPort(8082);/*from w w  w .  j av a  2s .  c om*/
    initializeRoutes();
}

From source file:ARS.DBManager.java

public void insertUser(User uObj) {
    try {// w w w.  j  ava  2s.c  o  m
        MongoClient mongoClient = new MongoClient("localhost", 27017);
        //Connecting

        MongoDatabase db = mongoClient.getDatabase("ars");
        System.out.println("Connecting to the db...");
        MongoCollection collection = db.getCollection("users"); //Choosing the collection to insert
        System.out.println(collection);

        Document inputObj = new Document();
        // inputObj.put("_id", 1123);
        inputObj.put("first-name", uObj.getFirstName());
        inputObj.put("last-name", uObj.getLastName());
        inputObj.put("gender", uObj.getGender());
        inputObj.put("nationality", uObj.getNation());
        inputObj.put("phone-number", uObj.getPhoneNumber());
        inputObj.put("email", uObj.geteMail());
        inputObj.put("password", uObj.getPassword());
        inputObj.put("user-type", "customer");
        collection.insertOne(inputObj);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:ARS.DBManager.java

public boolean findUserByName(String name) {
    User uObj;/*from   w  ww. jav a 2  s  . c  o  m*/
    int counter = 0;

    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("ars");
    MongoCollection collection = db.getCollection("users"); //Choosing the collection to insert

    BasicDBObject query = new BasicDBObject();
    query.put("first-name", name);
    MongoCursor cursor = collection.find(query).iterator();

    //Counter gives one if it's give the result
    while (cursor.hasNext()) {
        System.out.println(cursor.next());
        counter++;
    }

    if (counter == 1) {
        return true;
    } else {
        return false;
    }
}

From source file:ARS.DBManager.java

public boolean deleteUser(String firstName) {
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    MongoDatabase db = mongoClient.getDatabase("ars");
    MongoCollection collection = db.getCollection("users"); //Choosing the collection to insert

    BasicDBObject searchQuery = new BasicDBObject();
    searchQuery.put("first-name", firstName);

    collection.deleteOne(searchQuery);//ww  w .j a  v  a2  s . c o  m

    if (findUserByName(firstName) == false) {
        return true;
    } else
        return false;

}