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:com.tml.pathummoto.Dao.PartDao.java

public void addPart(Part part) {
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("part");

    BasicDBObject doc = new BasicDBObject("title", "part").append("_id", part.getPartNo())
            .append("Part Name", part.getPartName()).append("Model Name", part.getModelName())
            .append("Image No", part.getImageNo()).append("Part Type", part.getPartType()).append("quant", 0);

    try {/*from  w  w w  . j a v a  2s. co  m*/
        coll.insert(doc);
    } catch (Exception e) {
        System.out.println("Document inserted successfully");
    }
}

From source file:com.tml.pathummoto.Dao.PartDao.java

public Part searchPart(String partNo) {
    Part part = new Part();
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("part");
    System.out.println("Collection part selected successfully");

    BasicDBObject find = new BasicDBObject();
    find.put("_id", partNo);
    DBCursor cursor = coll.find(find);/*w  ww .  j a va2 s  . co  m*/
    while (cursor.hasNext()) {
        System.out.println(cursor.next());
    }
    BasicDBObject doc = (BasicDBObject) cursor.curr();
    if (doc != null) {

    }

    return part;

}

From source file:com.tml.pathummoto.Dao.PartDao.java

public Part singlePart(String partNumber) {
    Part part = new Part();
    // To connect to mongodb server
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("part");
    System.out.println("Collection user selected successfully");

    BasicDBObject whereQuery = new BasicDBObject();
    whereQuery.put("_id", partNumber);
    DBCursor cursor = coll.find(whereQuery);
    while (cursor.hasNext()) {
        System.out.println(cursor.next());
    }/*from   w  w w.  ja  v  a 2s. c  o  m*/
    BasicDBObject doc = (BasicDBObject) cursor.curr();
    if (doc != null) {
        part.setPartName(doc.getString("Part Name"));
        part.setPartNo(doc.getString("_id"));

    }

    return part;
}

From source file:com.tml.pathummoto.Dao.PartDao.java

public void addMainPart(MainPart mainpart) {
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("mainpart");

    BasicDBObject doc = new BasicDBObject("title", "mainpart").append("path", mainpart.getImageName())
            .append("Model Name", mainpart.getModelName()).append("Part Type", mainpart.getPartType());

    coll.insert(doc);//  ww  w  .  j  av a  2 s  . co  m
    System.out.println("Document inserted successfully");

}

From source file:com.tml.pathummoto.Dao.PartDao.java

public ArrayList<String> getModelData() {
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    BasicDBObject query = new BasicDBObject();
    BasicDBObject field = new BasicDBObject();
    field.put("_id", 1);
    DBCursor cursor = db.getCollection("model").find(query, field);
    ArrayList arr = new ArrayList();
    while (cursor.hasNext()) {
        BasicDBObject obj = (BasicDBObject) cursor.next();
        arr.add(obj.getString("_id"));
    }//from w  ww . j av  a2s.c o m
    return (arr);
}

From source file:com.tml.pathummoto.Dao.PartDao.java

public ArrayList getMainPartData(String name) {
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("mainpart");
    System.out.println("Collection user selected successfully");

    BasicDBObject whereQuery = new BasicDBObject();
    whereQuery.put("Model Name", name);
    DBCursor cursor = coll.find(whereQuery);
    ArrayList arr = new ArrayList();
    while (cursor.hasNext()) {
        BasicDBObject obj = (BasicDBObject) cursor.next();
        arr.add(obj.getString("Part Type"));
    }/*from w  w w.  j a va  2 s.  co  m*/
    return (arr);
}

From source file:com.tml.pathummoto.Dao.PartDao.java

public String getImage(String model, String mainPart) {
    String path = "C:\\Users\\Tishan Madhawa\\Pictures\\WP_20160118_17_31_47_Pro.jpg";
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("mainpart");
    System.out.println("Collection user selected successfully");

    BasicDBObject whereQuery = new BasicDBObject();
    whereQuery.put("Model Name", model);
    whereQuery.put("Part Type", mainPart);
    DBCursor cursor = coll.find(whereQuery);

    while (cursor.hasNext()) {
        BasicDBObject obj = (BasicDBObject) cursor.next();

        path = obj.getString("path");

    }//www . j av  a 2  s . com

    System.out.println(path);
    return path;
}

From source file:com.tml.pathummoto.Dao.PartDao.java

public ArrayList searchParts(String model, String mainPart) {
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("part");
    System.out.println("Collection user selected successfully");

    BasicDBObject whereQuery = new BasicDBObject();
    whereQuery.put("Model Name", model);
    whereQuery.put("Part Type", mainPart);

    DBCursor cursor = coll.find(whereQuery);
    ArrayList<Part> arr = new ArrayList<Part>();
    while (cursor.hasNext()) {
        BasicDBObject obj = (BasicDBObject) cursor.next();
        Part part = new Part(obj.getString("Image No"), obj.getString("_id"), obj.getString("Part Name"));

        arr.add(part);// ww  w  .  j a va 2s  .  c o  m
        System.out.println(arr);
    }
    return (arr);
}

From source file:com.tml.pathummoto.Dao.PartDao.java

public ArrayList<String> getMainPart() {
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    BasicDBObject query = new BasicDBObject();
    BasicDBObject field = new BasicDBObject();
    field.put("Part Type", 1);
    DBCursor cursor = db.getCollection("mainpart").find(query, field);
    ArrayList arr = new ArrayList();
    while (cursor.hasNext()) {
        BasicDBObject obj = (BasicDBObject) cursor.next();
        arr.add(obj.getString("Part Type"));
    }//from   w  w w. ja  va  2s. c  o m
    return (arr);
}

From source file:com.tml.pathummoto.Dao.PartDao.java

public ArrayList searchpart3(String modelName, String PartType) {
    MongoClient mongoClient = new MongoClient("localhost", 27017);

    // Now connect to your databases
    DB db = mongoClient.getDB("pathumdb");
    System.out.println("Connect to database successfully");

    DBCollection coll = db.getCollection("part");
    System.out.println("Collection user selected successfully");
    System.out.println(modelName);
    System.out.println(PartType);
    BasicDBObject whereQuery = new BasicDBObject();
    whereQuery.put("Model Name", modelName);
    whereQuery.put("Part Type", PartType);
    DBCursor cursor = coll.find(whereQuery);
    ArrayList<Part> arr = new ArrayList<Part>();
    while (cursor.hasNext()) {
        BasicDBObject obj = (BasicDBObject) cursor.next();
        Part part = new Part(obj.getString("Part Name"), obj.getString("_id"), obj.getString("Part Type"),
                obj.getInt("quant"));
        // System.out.println(obj.getString("Image No").toString());
        arr.add(part);/*from   ww w.  j  a v  a  2 s  . c o m*/

    }
    return (arr);

}