Example usage for com.mongodb MongoClient getDB

List of usage examples for com.mongodb MongoClient getDB

Introduction

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

Prototype

@Deprecated 
public DB getDB(final String dbName) 

Source Link

Document

Gets a database object.

Usage

From source file:gMIRC_handler.java

/**
 * Moved active user to a passive user (soon to be deleted)
 *
 * @param username// w  w w  .  j a va2  s . c  o  m
 * @return code
 */
public static int SoftDelete(String username) {
    int ret = 0;
    try {
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("activeUser");
        DBCollection coll2 = db.getCollection("passiveUser");
        BasicDBObject query = new BasicDBObject("username", username);

        DBCursor cursor = coll.find(query);

        try {
            if (cursor.hasNext()) {
                DBObject temp = cursor.next();
                coll2.insert(temp);
                coll.remove(temp);
            } else {
                ret = 1;
            }
        } finally {
            cursor.close();
        }
    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return ret;
}

From source file:gMIRC_handler.java

public String GetMessage(String username) {
    String ret = "";
    try {/* w w w  . j  a v  a2 s . c o  m*/
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("inbox");
        BasicDBObject query = new BasicDBObject("target", username);
        JSONObject obj = new JSONObject();
        JSONArray arr = new JSONArray();
        DBCursor cursor = coll.find(query);

        try {
            while (cursor.hasNext()) {
                BasicDBObject temp = (BasicDBObject) cursor.next();
                JSONObject sav = new JSONObject();
                sav.put("target", temp.getString("target"));
                sav.put("username", temp.getString("username"));
                sav.put("channel", temp.getString("channel"));
                sav.put("message", temp.getString("message"));
                sav.put("timestamp", temp.getLong("timestamp"));
                arr.add(sav);
                coll.remove(temp);
            }
            obj.put("msg", arr);
            ret = obj.toJSONString();
        } finally {
            cursor.close();
        }
    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    UpdateLastActive(username);
    return ret;
}

From source file:gMIRC_handler.java

private int PutMessage(String username, String channelname, String msg) {
    int ret = 0;// w  w w .  j  ava 2  s.c  o m
    try {
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("inbox");
        DBCollection coll2 = db.getCollection("channelCollection");
        BasicDBObject query2 = new BasicDBObject("channel", channelname);
        BasicDBObject query = new BasicDBObject("channel", channelname).append("username", username);
        DBCursor cursor = coll2.find(query);
        try {
            if (cursor.hasNext()) {
                DBCursor cursor2 = coll2.find(query2);
                System.out.println("Got message from " + username);
                try {
                    java.util.Date date = new java.util.Date();
                    while (cursor2.hasNext()) {
                        ret = 1;
                        BasicDBObject temp = (BasicDBObject) cursor2.next();
                        String target = temp.get("username").toString();
                        BasicDBObject put = new BasicDBObject("target", target).append("username", username)
                                .append("channel", channelname).append("message", msg)
                                .append("timestamp", date.getTime());
                        coll.insert(put);
                        ret = 0;
                    }
                } finally {
                    cursor2.close();
                }
            } else {
                ret = 2;
                System.out.println(username + " not registered to Channel : " + channelname);
            }
        } finally {
            cursor.close();
        }

    } catch (UnknownHostException ex) {
        ret = 1;
        Logger.getLogger(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    return ret;
}

From source file:gMIRC_handler.java

private int PutMessageWild(String username, String msg) {
    int ret = 1;/*from   ww w  .jav a2  s.c om*/

    try {
        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll2 = db.getCollection("channelCollection");
        BasicDBObject query = new BasicDBObject("username", username);
        System.out.println("Wild message appear from " + username + " !");
        DBCursor cursor = coll2.find(query);
        try {
            while (cursor.hasNext()) {
                ret = 1;
                BasicDBObject temp = (BasicDBObject) cursor.next();
                String channelname = temp.getString("channel");
                ret = ret & PutMessage(username, channelname, msg);
            }
        } finally {
            cursor.close();
        }
    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }

    return ret;
}

From source file:admin_change_Emp.java

private void update_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_update_buttonActionPerformed
    // TODO add your handling code here:

    try {//from w w w.  j  a v  a  2  s. c  o  m
        userid = userid_field.getText();
        int flag = 0;
        MongoClient mc = new MongoClient("localhost", 27017);
        DB db = mc.getDB("parking_system");
        DBCollection collection = db.getCollection("employee_info");

        BasicDBObject query = new BasicDBObject();
        BasicDBObject field = new BasicDBObject();
        field.put("_id", userid);

        DBCursor cursor = collection.find(query, field);
        while (cursor.hasNext()) {
            DBObject obj = (DBObject) cursor.next();
            userid_db = obj.get("_id").toString();

            if (userid.equals(userid_db)) {

                flag = 1;
                setVisible(false);
                new admin_update_Emp(userid).setVisible(true);
                break;
            }
        }
        if (flag == 0) {
            JOptionPane.showMessageDialog(null, "UserID invalid");
        }

    } catch (Exception e) {

    }

}

From source file:admin_change_Emp.java

private void delete_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_delete_buttonActionPerformed
    // TODO add your handling code here:

    try {/*w w  w  . j a v  a  2s . c  o m*/
        userid = userid_field.getText();
        int flag = 0;

        MongoClient mc = new MongoClient("localhost", 27017);
        DB db = mc.getDB("parking_system");
        DBCollection collection = db.getCollection("employee_info");

        BasicDBObject query = new BasicDBObject();
        BasicDBObject field = new BasicDBObject();
        field.put("_id", userid);

        DBCursor cursor = collection.find(query, field);
        while (cursor.hasNext()) {
            DBObject obj = (DBObject) cursor.next();
            userid_db = obj.get("_id").toString();

            if (userid.equals(userid_db)) {

                flag = 1;

                BasicDBObject deleteObject = new BasicDBObject();
                deleteObject.put("_id", userid);
                DBCursor findQuery = collection.find(deleteObject);
                collection.remove(deleteObject);

                JOptionPane.showMessageDialog(null, "Employee Entry Deleted !!!");

                setVisible(false);
                new Admin_rights().setVisible(true);
                break;
            }
        }
        if (flag == 0) {
            JOptionPane.showMessageDialog(null, "UserID invalid");
        }
    } catch (Exception e) {

    }
}

From source file:fList.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*w w w.j a va 2s  . c om*/
 *
 * @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 {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter pw = response.getWriter();
    response.setContentType("text/html");

    String username = request.getParameter("username");

    try {
        MongoClient mongo = new MongoClient("localhost", 27017);
        DB db = mongo.getDB("test");
        System.out.println("Connection established");
        DBCollection User = db.getCollection("User");

        DBCursor cursor = User.find();

        while (cursor.hasNext()) {
            DBObject result = cursor.next();
            String s = result.get("username").toString();
            if (!s.equals(username)) {
                response.getWriter().write(s);
                response.getWriter().write(" ");
            }

        }

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:tweetList.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from   ww w . j a  va2  s .c  o  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 {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    response.setContentType("text/html");
    String username = request.getParameter("username");

    try {
        MongoClient mongo = new MongoClient("localhost", 27017);
        DB db = mongo.getDB("test");
        System.out.println("Connection established");
        DBCollection User = db.getCollection("User");
        DBCollection Tweet = db.getCollection("Tweet");

        BasicDBObject query = new BasicDBObject("username", username);
        DBCursor curssc = User.find(query);
        // out.println(curssc.size());
        DBObject result = curssc.next();
        ArrayList<String> arr2 = new ArrayList();
        arr2 = (ArrayList<String>) result.get("tweet_id");

        ArrayList<String> e = new ArrayList();
        e = (ArrayList<String>) result.get("following_username");
        ArrayList<String> texts = new ArrayList();
        //out.println(e.size());
        //out.println(e.get(0));

        if (e.size() > 0 || arr2.size() > 0) {
            for (int i = e.size() - 1; i >= 0; i--) {
                BasicDBObject query2 = new BasicDBObject("username", e.get(i));
                DBCursor curssc2 = User.find(query2);
                if (curssc2.size() == 1) {
                    DBObject res = curssc2.next();
                    ArrayList<String> arr = new ArrayList();
                    arr = (ArrayList<String>) res.get("tweet_id");

                    texts.addAll(arr);

                }

            }
            texts.addAll(arr2);
            Collections.sort(texts);
            for (int j = texts.size() - 1; j >= 0; j--) {
                BasicDBObject query3 = new BasicDBObject("tweet_id", texts.get(j));
                DBCursor curssc3 = Tweet.find(query3);
                DBObject result1 = curssc3.next();
                response.getWriter().write(result1.get("username").toString());
                response.getWriter().write(":  ");
                response.getWriter().write(result1.get("tweet_text").toString());
                response.getWriter().write("\n");
            }

        } else {
            response.getWriter().write("No tweet");
        }

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:filterFriends.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from   ww w.j  a  v a2  s . c o  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 {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter pw = response.getWriter();
    response.setContentType("text/html");

    String username = request.getParameter("username");
    String filtered_name = request.getParameter("filtered");

    try {
        MongoClient mongo = new MongoClient("localhost", 27017);
        DB db = mongo.getDB("test");
        System.out.println("Connection established");
        DBCollection User = db.getCollection("User");

        DBCursor cursor = User.find();

        while (cursor.hasNext()) {
            DBObject result = cursor.next();
            String s = result.get("username").toString();
            if (s.equals(filtered_name)) {
                response.getWriter().write(s);
                response.getWriter().write(" ");
            }

        }

    } catch (Exception e) {
        System.err.println(e.getClass().getName() + ": " + e.getMessage());
    }
}

From source file:UnitTest3.java

License:Open Source License

public static int testgridfs() {

    long time1;/*from   w w w . j  a v  a2 s.  c  o  m*/
    long time2;
    long time3;
    long time4;

    try {

        MongoClient mongo = new MongoClient("localhost", 27017);
        DB db = mongo.getDB("JFileDB");

        // TODO JFileMetaDataTable should be in MetaDB database
        DBCollection collection = db.getCollection("JFileMetaDataTable");

        String newFileName = "com.dilmus.scabi.testdata.in.App.class";

        File jFile = new File("/home/anees/workspace/testdata/in/App.class");

        // create a JFileTable namespace
        GridFS gfsj = new GridFS(db, "JFileTable");

        // get file from local drive
        GridFSInputFile gfsFile = gfsj.createFile(jFile);

        // set a new filename for identify purpose
        gfsFile.setFilename(newFileName);
        gfsFile.setContentType("class"); // jar, zip, war
        // save the image file into mongoDB
        gfsFile.save();

        // Let's create a new JSON document with some "metadata" information
        BasicDBObject info = new BasicDBObject();
        info.put("DBHost", "localhost");
        info.put("DBPort", "27017");
        info.put("JFileName", newFileName);
        info.put("JFileID", gfsFile.getId());
        info.put("JFileMD5", gfsFile.getMD5());
        collection.insert(info, WriteConcern.ACKNOWLEDGED);

        // print the result
        DBCursor cursor = gfsj.getFileList();
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }

        DBCursor cursor2 = collection.find();

        while (cursor2.hasNext()) {
            System.out.println(cursor2.next());
        }

        // get file by it's filename
        GridFSDBFile jForOutput = gfsj.findOne(newFileName);

        // save it into a new image file
        jForOutput.writeTo("/home/anees/workspace/testdata/out/AppOut.class");

        // remove the file from mongoDB
        // gfsj.remove(gfsj.findOne(newFileName));

        System.out.println("Done");
        mongo.close();
    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (MongoException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return 0;
}