Example usage for com.mongodb Mongo Mongo

List of usage examples for com.mongodb Mongo Mongo

Introduction

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

Prototype

Mongo(final MongoClientURI mongoURI, @Nullable final MongoDriverInformation mongoDriverInformation) 

Source Link

Usage

From source file:PanMostrar.java

/**
 * Creates new form PanMostrar/*from  www  . j  a v  a  2s . c om*/
 */

public PanMostrar() {

    //Conexion a mongoDb
    try {
        Mongo mongo = new Mongo("localhost", 27017);
        db = mongo.getDB("Escuela");
        tabla = db.getCollection("Alumnos");

    } catch (UnknownHostException ex) {
        Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex);
    }

    initComponents();

    int num = 1;
    DBCursor cursor = tabla.find();
    DefaultListModel model = new DefaultListModel();

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

        // Obtener variables del JSON
        try {

            String name = (String) obj.get("Nombre");
            String ap = (String) obj.get("ApPaterno");
            String am = (String) obj.get("ApMaterno");
            String fechaNac = (String) obj.get("FechaNac");
            int edad = (int) obj.get("Edad");
            String sex = (String) obj.get("Sexo");
            String mat = (String) obj.get("Matricula");
            int semestre = (int) obj.get("Semestre");
            String carrera = (String) obj.get("Carrera");

            // Imprimir todo
            /*Mostrar.setText(Mostrar.getText() + "\n" + name +  " " + ap + " " + am + " " 
                + fechaNac + " " + edad + " " + sex + " " + mat + " " + semestre + 
                " " + carrera);*/

            model.addElement(num + "  " + name + " " + ap + " " + am + " " + fechaNac + " " + edad + " " + sex
                    + " " + mat + " " + semestre + " " + carrera);
            num++;

        } catch (Exception ex) {
            System.out.println("objeto no completo");
        }
        Lista.setModel(model);
    }
}

From source file:search.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from   w  w w  .j av  a2 s .  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");
    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 search</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet search at " + request.getContextPath() + "</h1>");
        String srch_id = request.getParameter("search");
        DB db;

        Mongo mongo = new Mongo("LocalHost", 27017);
        db = mongo.getDB("local");
        out.println("db connected is..." + db);
        DBCollection collection = db.getCollection("StartupHun");
        out.println("table used connected is..." + collection);
        BasicDBObject query = new BasicDBObject("founded_year", new BasicDBObject("$gt", 2005));
        BasicDBObject cat = new BasicDBObject();
        //query.put("name", srch_id);
        DBCursor cursor = collection.find();
        // Print out "highest" priority items
        BasicDBObject query1 = null;
        int i = 0;
        while (cursor.hasNext()) {
            //out.println(cursor.next());
            query1 = (BasicDBObject) cursor.next();

            out.println((Integer) query1.get("founded_year") + "  " + i);
            i++;
        }

        out.println("</body>");
        out.println("</html>");
    }
}

From source file:PanModificar.java

public PanModificar() {
    //Conexion a mongoDb
    try {/*  w ww. j  ava  2s.co m*/
        Mongo mongo = new Mongo("localhost", 27017);
        db = mongo.getDB("Escuela");
        tabla = db.getCollection("Alumnos");

    } catch (UnknownHostException ex) {
        Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex);
    }
    DBCursor cursor = tabla.find();

    ArrayList rows = new ArrayList();
    String[] titulos = { "Nombre", "ApPaterno", "ApMaterno", "FechaNac", "Edad", "Sexo", "Matricula",
            "Semestre", "Carrera" };
    ArrayList<String> columndata = new ArrayList<String>(Arrays.asList(titulos));
    while (cursor.hasNext()) {
        BSONObject obj = cursor.next();

        // Obtener variables del JSON
        try {

            String name = (String) obj.get("Nombre");
            String ap = (String) obj.get("ApPaterno");
            String am = (String) obj.get("ApMaterno");
            String fechaNac = (String) obj.get("FechaNac");
            int edad = (int) obj.get("Edad");
            String sex = (String) obj.get("Sexo");
            String mat = (String) obj.get("Matricula");
            int semestre = (int) obj.get("Semestre");
            String carrera = (String) obj.get("Carrera");
            ArrayList<String> row = new ArrayList<String>(Arrays.asList(
                    new String[] { name, ap, am, fechaNac, "" + edad, sex, mat, "" + semestre, carrera }));
            rows.add(row);
        } catch (Exception ex) {
            System.out.println("objeto no completo");
        }

    }

    initComponents();
    jTable1.setModel(new CustomTableModel(columndata, rows, "no hay datos en la bd!!"));
    jTable1.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
}

From source file:Modificar.java

/**
 * Creates new form Inicio//w ww.j av  a 2 s  .co  m
 */
public Modificar(Object[] columnnames, Object[] data) {

    initComponents();
    //Conexion a mongoDb
    try {
        Mongo mongo = new Mongo("localhost", 27017);
        db = mongo.getDB("Escuela");
        collection = db.getCollection("Alumnos");
        this.columnnames = columnnames;
        this.data = data;
        NombreT.setText((String) data[0]);
        ApPaterno.setText((String) data[1]);
        ApMaterno.setText((String) data[2]);
        dia.setSelectedItem(((String) data[3]).split("-")[0]);
        mes.setSelectedItem(((String) data[3]).split("-")[1]);
        anio.setSelectedItem(((String) data[3]).split("-")[2]);
        EdadT.setText((String) data[4]);
        sexo.setSelectedItem((String) data[5]);
        matricula.setText((String) data[6]);
        semestre.setSelectedItem((String) data[7]);
        Carrera.setText((String) data[8]);

    } catch (UnknownHostException ex) {
        Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex);
    }

}

From source file:Ventana.java

public Ventana() {

    this.setTitle("Mexico SIN Hambre");
    initComponents();/* w  ww. ja  v  a 2 s.  c om*/
    this.getContentPane().setBackground(Color.WHITE);
    this.setBounds(0, 0, 345, 51);
    setResizable(false);

    fields[0] = jLabel1;
    fields[1] = jLabel2;
    fields[2] = jLabel3;
    fields[3] = jLabel4;
    fields[4] = jLabel5;
    fields[5] = jLabel6;
    fields[6] = jLabel7;
    fields[7] = jLabel8;
    fields[8] = jLabel9;
    fields[9] = jLabel10;
    fields[10] = jLabel11;
    fields[11] = jLabel12;

    texts[0] = jTextField1;
    texts[1] = jTextField2;
    texts[2] = jTextField3;
    texts[3] = jTextField4;
    texts[4] = jTextField5;
    texts[5] = jTextField6;
    texts[6] = jTextField7;
    texts[7] = jTextField8;
    texts[8] = jTextField9;
    texts[9] = jTextField10;
    texts[10] = jTextField11;
    texts[11] = jTextField12;

    add_to_ActionListenner();
    make_lab_and_text_invisible();

    try {
        Mongo mongo = new Mongo("localhost", 27017);

        db = mongo.getDB("Beneficiarios");

    } catch (UnknownHostException ex) {
        Logger.getLogger(Form.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:PanBorrar.java

/**
 * Creates new form PanBorrar//from  w  ww.  j  av a2s  .  co  m
 */
public PanBorrar() {
    try {
        Mongo mongo = new Mongo("localhost", 27017);
        db = mongo.getDB("Escuela");
        tabla = db.getCollection("Alumnos");

    } catch (UnknownHostException ex) {
        Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex);
    }

    initComponents();
}

From source file:Inicio.java

/**
 * Creates new form Inicio//from   w  w w . j  a va  2 s  . com
 */
public Inicio() {

    //Conexion a mongoDb
    try {
        Mongo mongo = new Mongo("localhost", 27017);
        db = mongo.getDB("Escuela");
        tabla = db.getCollection("Alumnos");

    } catch (UnknownHostException ex) {
        Logger.getLogger(Inicio.class.getName()).log(Level.SEVERE, null, ex);
    }

    initComponents();
}

From source file:act.server.MongoDB.java

License:Open Source License

public static void dropDB(String mongoActHost, int port, String dbs, boolean force) {
    try {/*from   ww  w. j  a  v a 2s .  co  m*/
        DB toDropDB = new Mongo(mongoActHost, port).getDB(dbs);

        if (!force) {
            // Require explicit confirmation from the user before dropping an existing DB.
            System.out.format(
                    "Going to drop: %s:%d/%s. Type \"DROP\" (without quotes) and press enter to proceed.\n",
                    mongoActHost, port, dbs);
            try (BufferedReader reader = new BufferedReader(new InputStreamReader(System.in))) {
                String readLine = reader.readLine();
                if (!"DROP".equals(readLine)) {
                    System.out.format("Invalid input \"%s\", not dropping DB\n", readLine);
                } else {
                    System.out.format("Dropping DB\n");
                    // drop DB!
                    toDropDB.dropDatabase();
                }
            }
        } else {
            System.out.format("[Force] Dropping DB %s\n", dbs);
            toDropDB.dropDatabase();
        }
    } catch (UnknownHostException e) {
        throw new IllegalArgumentException("Invalid host for Mongo Act server.");
    } catch (MongoException e) {
        throw new IllegalArgumentException("Could not initialize Mongo driver.");
    } catch (IOException e) {
        throw new RuntimeException("Unable to read from stdin");
    }
}

From source file:act.server.MongoDB.java

License:Open Source License

private void initDB() {
    try {/*ww w. j  a v  a2 s .  c  om*/
        mongo = new Mongo(this.hostname, this.port);
        mongoDB = mongo.getDB(this.database);

        // in case the db is protected then we would do the following:
        // boolean auth = db.authenticate(myUserName, myPassword);
        // but right now we do not care.

        this.dbReactions = mongoDB.getCollection("reactions");
        this.dbChemicals = mongoDB.getCollection("chemicals");
        this.dbCofactors = mongoDB.getCollection("cofactors");
        this.dbOrganisms = mongoDB.getCollection("organisms");
        this.dbOrganismNames = mongoDB.getCollection("organismnames");
        this.dbSeq = mongoDB.getCollection("seq");
        this.dbCascades = mongoDB.getCollection("cascades");
        this.dbWaterfalls = mongoDB.getCollection("waterfalls");
        this.dbPubmed = mongoDB.getCollection("pubmed");

        initIndices();
    } catch (UnknownHostException e) {
        throw new IllegalArgumentException("Invalid host for Mongo Act server.");
    } catch (MongoException e) {
        throw new IllegalArgumentException(
                String.format("Could not initialize Mongo driver: %s", e.getMessage()));
    }
}

From source file:act.server.MongoDB.java

License:Open Source License

public static P<P<List, List>, Map<Object, Object>> compare(String coll, String id_key, int thisport,
        int refport, boolean listsAreSet) throws UnknownHostException {
    String host = "localhost";
    String dbs = "actv01";

    List<Object> add = new ArrayList<Object>();
    List<Object> del = new ArrayList<Object>();
    Set<Object> seen = new HashSet<Object>();
    Map<Object, Object> upd = new HashMap<Object, Object>();

    DBCollection c = new Mongo(host, thisport).getDB(dbs).getCollection(coll);
    DBCollection cref = new Mongo(host, refport).getDB(dbs).getCollection(coll);

    // yes, we indeed need to iterate over the entire collection! so unrestricted find() ok here.
    DBCursor cur = c.find();//from w w  w  .j  av  a  2 s  .com
    while (cur.hasNext()) {
        DBObject doc = cur.next();
        Object id = doc.get(id_key);

        DBObject docref = findOneDoc(cref, id_key, id);
        if (docref == null) {
            // reference collection does not have doc, log as newly created
            add.add(id);
        } else {
            // reference collection has doc:
            // compare the differences between these two docs and log it as updated if they differ

            Object diff = compare(doc, docref, listsAreSet);
            if (diff != null) {
                // the docs differ. Log it as updated, and note the diff
                upd.put(id, diff);
            }
        }
        seen.add(id);
    }

    // now iterate over ref db and see if there are any docs deleted (i.e., not in notDeleted)
    DBCursor curref = c.find();
    while (curref.hasNext()) {
        DBObject doc = curref.next();
        Object id = doc.get(id_key);

        if (!seen.contains(id)) {
            // this doc was not seen in the updated collection, so deleted. log that
            del.add(id);
        }
    }

    return new P<P<List, List>, Map<Object, Object>>(new P<List, List>(add, del), upd);
}