Example usage for com.mongodb DBCursor hasNext

List of usage examples for com.mongodb DBCursor hasNext

Introduction

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

Prototype

@Override
public boolean hasNext() 

Source Link

Document

Checks if there is another object available.

Usage

From source file:PanMostrar.java

/**
 * Creates new form PanMostrar/*from   www. jav  a  2 s. c o m*/
 */

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:Vehicle_exit.java

public void fetch() {
    try {//from ww  w . ja v  a2 s. com
        MongoClient mc = new MongoClient("localhost", 27017);
        DB db = mc.getDB("parking_system");
        DBCollection collectionx = db.getCollection("entry_info");

        if (veh_count == 2) {

            BasicDBObject queryx = new BasicDBObject();
            BasicDBObject fieldx = new BasicDBObject();
            fieldx.put("vehno", 1);
            DBCursor cursorx = collectionx.find(queryx, fieldx);

            while (cursorx.hasNext()) {

                System.out.println("hmmmWOW---n");
                BasicDBObject objx = (BasicDBObject) cursorx.next();

                char[] temp;
                temp = objx.getString("vehno").toCharArray();
                char last;
                last = temp[temp.length - 1];

                if (last == '2') {

                    vehno_combo.addItem(objx.get("vehno").toString());

                }
                System.out.println("hello");

            }
        } else if (veh_count == 4) {

            BasicDBObject queryx = new BasicDBObject();
            BasicDBObject fieldx = new BasicDBObject();
            fieldx.put("vehno", 1);
            DBCursor cursorx = collectionx.find(queryx, fieldx);

            while (cursorx.hasNext()) {

                System.out.println("hmmmWOW---n");
                BasicDBObject objx = (BasicDBObject) cursorx.next();

                char[] temp;
                temp = objx.getString("vehno").toCharArray();
                char last;
                last = temp[temp.length - 1];

                if (last == '4') {

                    vehno_combo.addItem(objx.get("vehno").toString());
                    //vehno = vehno_combo.getSelectedItem().toString();
                }
                System.out.println("hello");

            }
        }

        else {

            BasicDBObject queryx = new BasicDBObject();
            BasicDBObject fieldx = new BasicDBObject();
            fieldx.put("vehno", 1);
            DBCursor cursorx = collectionx.find(queryx, fieldx);

            while (cursorx.hasNext()) {

                System.out.println("hmmmWOW---n");
                BasicDBObject objx = (BasicDBObject) cursorx.next();

                char[] temp;
                temp = objx.getString("vehno").toCharArray();
                char last;
                last = temp[temp.length - 1];

                if (last == '6') {

                    vehno_combo.addItem(objx.get("vehno").toString());
                    //vehno = vehno_combo.getSelectedItem().toString();
                }
                System.out.println("hello");

            }
        }
    } catch (Exception e) {
    }
}

From source file:Vehicle_exit.java

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

    try {//from  ww w .j  av a  2  s .  c om
        vehno = vehno_combo.getSelectedItem().toString();
        vehno_copy = vehno;
        System.out.println("in button number niald" + vehno);

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

        BasicDBObject query_time = new BasicDBObject();
        BasicDBObject field_time = new BasicDBObject();
        field_time.put("intime", 1);
        DBCursor cursor = collection.find(query_time, field_time);

        BasicDBObject query_token = new BasicDBObject();
        BasicDBObject field_token = new BasicDBObject();
        field_time.put("vehno", 1);
        DBCursor cursor_token = collection.find(query_token, field_token);

        while (cursor.hasNext()) {
            BasicDBObject obj_time = (BasicDBObject) cursor.next();
            in_time_mills = Long.parseLong(obj_time.getString("intime"));

            BasicDBObject obj_token = (BasicDBObject) cursor_token.next();
            //db_token = obj_token.getInt("_id");
            vehno_db = obj_token.getString("vehno");
            System.out.println(vehno_db);
            System.out.println("hello");

            System.out.println("hey ayu");

            DBCollection collection1 = db.getCollection("vehicle_count");
            DBCollection collection2 = db.getCollection("statistics");
            vehno = vehno_copy;
            if (veh_count == 2) {
                //vehno = vehno + "W2";

                if (vehno.equals(vehno_db)) {

                    flag = 1;

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

                    int count_bike;
                    BasicDBObject query = new BasicDBObject();
                    BasicDBObject field = new BasicDBObject();
                    field.put("wheeler_2", 1);
                    DBCursor cursor1 = collection1.find(query, field);

                    BasicDBObject obj = (BasicDBObject) cursor1.next();
                    count_bike = obj.getInt("wheeler_2");

                    System.out.println(count_bike);

                    BasicDBObject searchUpdate = new BasicDBObject();
                    searchUpdate.append("wheeler_2", count_bike);

                    BasicDBObject UpdateObject = new BasicDBObject();
                    UpdateObject.append("$set", new BasicDBObject("wheeler_2", count_bike - 1));

                    collection1.update(searchUpdate, UpdateObject);

                    System.out.println("Checking...");

                    time_diff = exit_time_mills - in_time_mills;
                    time_diff = time_diff / 1000;
                    time_diff = time_diff / 60;
                    System.out.println("difftime" + time_diff);
                    if (time_diff > 60)
                        charges = 20 + (time_diff) / 2;
                    else
                        charges = 20;

                    //updating session...

                    int income_bike;
                    BasicDBObject query2 = new BasicDBObject();
                    BasicDBObject field2 = new BasicDBObject();
                    field2.put("2wheeler_income", 1);
                    DBCursor cursor2 = collection2.find(query2, field2);

                    BasicDBObject obj2 = (BasicDBObject) cursor2.next();
                    income_bike = obj2.getInt("2wheeler_income");
                    System.out.println(income_bike);

                    BasicDBObject searchUpdate2 = new BasicDBObject();
                    searchUpdate2.append("2wheeler_income", income_bike);

                    BasicDBObject UpdateObject2 = new BasicDBObject();
                    UpdateObject2.append("$set", new BasicDBObject("2wheeler_income", income_bike + charges));

                    collection2.update(searchUpdate2, UpdateObject2);

                    JOptionPane.showMessageDialog(null, "Vehicle Out !\n" +
                    /*"In Time : " + in_time_mills + "\n" +*/
                            "Extra Parking Time : " + time_diff + "Min.\n" + "Charges : " + charges);

                    System.out.println("Checked...");

                    setVisible(false);
                    new Entry_OR_Exit().setVisible(true);
                    break;
                }
            }

            else if (veh_count == 4) {

                //vehno = vehno + "W4";

                if (vehno.equals(vehno_db)) {

                    flag = 1;

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

                    int count_car;

                    BasicDBObject query = new BasicDBObject();
                    BasicDBObject field = new BasicDBObject();
                    field.put("wheeler_4", 1);
                    DBCursor cursor1 = collection1.find(query, field);

                    BasicDBObject obj = (BasicDBObject) cursor1.next();
                    count_car = obj.getInt("wheeler_4");
                    System.out.println(count_car);

                    BasicDBObject searchUpdate = new BasicDBObject();
                    searchUpdate.append("wheeler_4", count_car);

                    BasicDBObject UpdateObject = new BasicDBObject();
                    UpdateObject.append("$set", new BasicDBObject("wheeler_4", count_car - 1));

                    collection1.update(searchUpdate, UpdateObject);

                    System.out.println("Checking...2");

                    time_diff = exit_time_mills - in_time_mills;
                    time_diff = time_diff / 1000;
                    time_diff = time_diff / 60;
                    System.out.println("difftime" + time_diff);
                    if (time_diff > 60)
                        charges = 40 + (time_diff) * 3 / 2;
                    else
                        charges = 40;

                    //Session update....

                    int income_car;
                    BasicDBObject query2 = new BasicDBObject();
                    BasicDBObject field2 = new BasicDBObject();
                    field2.put("4wheeler_income", 1);
                    DBCursor cursor2 = collection2.find(query2, field2);

                    BasicDBObject obj2 = (BasicDBObject) cursor2.next();
                    income_car = obj2.getInt("4wheeler_income");
                    System.out.println(income_car);

                    BasicDBObject searchUpdate2 = new BasicDBObject();
                    searchUpdate2.append("4wheeler_income", income_car);

                    BasicDBObject UpdateObject2 = new BasicDBObject();
                    UpdateObject2.append("$set", new BasicDBObject("4wheeler_income", income_car + charges));

                    collection2.update(searchUpdate2, UpdateObject2);

                    JOptionPane.showMessageDialog(null, "Vehicle Out !\n" +
                    /*"In Time : " + in_time_mills + "\n" +*/
                            "Extra Parking Time : " + time_diff + "Min.\n" + "Charges : " + charges);

                    System.out.println("Checking...2");

                    setVisible(false);
                    new Entry_OR_Exit().setVisible(true);
                    break;

                }
            } else {

                //vehno = vehno + "W6";
                if (vehno.equals(vehno_db)) {

                    flag = 1;

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

                    int count_other;

                    BasicDBObject query = new BasicDBObject();
                    BasicDBObject field = new BasicDBObject();
                    field.put("other", 1);
                    DBCursor cursor1 = collection1.find(query, field);

                    BasicDBObject obj = (BasicDBObject) cursor1.next();
                    count_other = obj.getInt("other");

                    System.out.println(count_other);

                    BasicDBObject searchUpdate = new BasicDBObject();
                    searchUpdate.append("other", count_other);

                    BasicDBObject UpdateObject = new BasicDBObject();
                    UpdateObject.append("$set", new BasicDBObject("other", count_other - 1));

                    collection1.update(searchUpdate, UpdateObject);

                    System.out.println("Checking...3");

                    time_diff = exit_time_mills - in_time_mills;
                    time_diff = time_diff / 1000;
                    time_diff = time_diff / 60;
                    System.out.println("difftime" + time_diff);
                    if (time_diff > 60)
                        charges = 60 + (time_diff);
                    else
                        charges = 60;

                    //sessiuon update....

                    int income_other;
                    BasicDBObject query2 = new BasicDBObject();
                    BasicDBObject field2 = new BasicDBObject();
                    field2.put("other_income", 1);
                    DBCursor cursor2 = collection2.find(query2, field2);

                    BasicDBObject obj2 = (BasicDBObject) cursor2.next();
                    income_other = obj2.getInt("other_income");
                    System.out.println(income_other);

                    BasicDBObject searchUpdate2 = new BasicDBObject();
                    searchUpdate2.append("other_income", income_other);

                    BasicDBObject UpdateObject2 = new BasicDBObject();
                    UpdateObject2.append("$set", new BasicDBObject("other_income", income_other + charges));

                    collection2.update(searchUpdate2, UpdateObject2);

                    JOptionPane.showMessageDialog(null, "Vehicle Out !\n" +
                    /*"In Time : " + in_time_mills + "\n" +*/
                            "Extra Parking Time : " + time_diff + "Min.\n" + "Charges : " + charges);

                    System.out.println("Checked...3");
                    setVisible(false);
                    new Entry_OR_Exit().setVisible(true);
                    break;

                }
            }
            //}
        }
        if (flag == 0) {
            JOptionPane.showMessageDialog(null, "Invalid Vehicle Number or Type");
            setVisible(false);
            new Entry_OR_Exit().setVisible(true);
        }
        /*
        BasicDBObject searchUpdate = new BasicDBObject();
        searchUpdate.append("wheeler_2", count_bike);
                
        BasicDBObject UpdateObject = new BasicDBObject();
        UpdateObject.append("$set", new BasicDBObject("wheeler_2", count_bike + 1));
                
        collection.update(searchUpdate, UpdateObject);
                
        */
        //}
    } catch (Exception e) {

    }

}

From source file:AllUser.java

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        al = new ArrayList<String>();
        // To connect to mongodb server
        MongoClient mongoClient = new MongoClient("localhost", 27017);

        // Now connect to your databases
        DB db = mongoClient.getDB("XHiring");
        System.out.println("Connect to database successfully");
        DBCollection coll = db.getCollection("users");
        System.out.println("Collection mycol selected successfully");

        DBCursor cursor = coll.find();
        int i = 1;
        try {/* w  w  w  . j  ava2s  .  c om*/
            while (cursor.hasNext()) {

                al.add(cursor.next().toString());
                i++;
            }
        } finally {
            System.out.println("arra: " + al);
            Genson obj = new Genson();
            out.println(obj.serialize(al));
        }

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

From source file:LoginUI.java

private HashMap<String, String> getUserInfo() throws ParseException {
    DBCollection collection = db.getCollection("Users");

    DBObject allquery = new BasicDBObject();
    DBObject fields = new BasicDBObject();
    fields.put("Info", 1);

    DBCursor infocursor = collection.find(allquery, fields);
    HashMap<String, String> userpw = new HashMap<String, String>();

    while (infocursor.hasNext()) {
        String json = infocursor.next().toString();
        JSONParser parser = new JSONParser();
        JSONObject jsonFile = (JSONObject) parser.parse(json);
        JSONObject structure = (JSONObject) jsonFile.get("Info");
        String user = structure.get("Username").toString();
        String pw = structure.get("Password").toString();
        JSONObject idStruct = (JSONObject) jsonFile.get("_id");
        String id = idStruct.get("$oid").toString();

        userpw.put(user, pw);/*from  w w  w  .  ja v a  2s .  c  o  m*/
    }
    return userpw;
}

From source file:gMIRC_handler.java

/**
 * Add a user to a channel (can check if the channel is a new channel)
 *
 * @param Username/*  w ww  .  j ava 2 s. c  om*/
 * @param Channel
 * @return code
 */
private int AddChannel(String Username, String Channel) {
    int ret = 0;
    try {

        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("channelCollection");
        BasicDBObject query = new BasicDBObject("username", Username).append("channel", Channel);

        DBCursor cursor = coll.find(query);

        try {
            if (cursor.hasNext()) {
                ret = 1;
                System.err.println(Username + " has joined Channel : " + Channel + "!");
            } else {
                query = new BasicDBObject("channel", Channel);
                DBCursor cursor2 = coll.find(query);
                try {
                    if (!cursor2.hasNext()) {
                        ret = 2;
                    }
                } finally {
                    cursor2.close();
                }
                BasicDBObject doc = new BasicDBObject("username", Username).append("channel", Channel);
                coll.insert(doc);
                System.out.println(Username + " joined Channel : " + Channel);
            }
        } 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

/**
 * Delete a user from a channel (used in leave method)
 *
 * @param Channel/*  w w  w. ja  va  2 s  .  c o m*/
 * @param Username
 * @return code
 */
private int DeleteChannelUser(String Channel, String Username) {
    int ret = 0;
    try {

        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("channelCollection");
        BasicDBObject query = new BasicDBObject("username", Username).append("channel", Channel);

        DBCursor cursor = coll.find(query);

        try {
            ret = 1;
            while (cursor.hasNext()) {
                ret = 1;
                coll.remove(cursor.next());
                ret = 0;
            }
        } finally {
            cursor.close();
        }

    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println(Username + " has leaved Channel : " + Channel);
    return ret;
}

From source file:gMIRC_handler.java

/**
 * Delete a user in all channel (used in cleaning)
 *
 * @param Username/*  w  ww . j  ava2  s  . c  om*/
 * @return code
 */
public int DeleteUserInChannel(String Username) {
    int ret = 0;
    try {

        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("channelCollection");
        BasicDBObject query = new BasicDBObject("username", Username);

        DBCursor cursor = coll.find(query);

        try {
            while (cursor.hasNext()) {
                coll.remove(cursor.next());
            }
        } finally {
            cursor.close();
        }

    } catch (UnknownHostException ex) {
        Logger.getLogger(gMIRC_handler.class.getName()).log(Level.SEVERE, null, ex);
    }
    System.out.println(Username + " has been deleted in Channel Collection!");
    return ret;
}

From source file:gMIRC_handler.java

/**
 * Register a user to the server (used in RegUser)
 *
 * @param username//  w  ww  .  j av a2 s. c  om
 * @return code
 */
private int AddUser(String username) {
    int ret = 0;
    try {

        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("activeUser");
        BasicDBObject query = new BasicDBObject("username", username);

        DBCursor cursor = coll.find(query);

        try {
            if (cursor.hasNext()) {
                Date now = new Date();
                long timestamp_now = now.getTime();
                long treshold = timestamp_now - (1000 * 10); //10
                BasicDBObject temp = (BasicDBObject) cursor.next();
                Date time_temp = (Date) temp.get("timestamp");
                long timestamp_temp = time_temp.getTime();
                if (timestamp_temp < treshold) {
                    ret = 2;
                    System.out.println(username + " has joined back!");
                } else {
                    ret = 1;
                    System.err.println(username + " has been used !");
                }
            } else {
                java.util.Date date = new java.util.Date();
                BasicDBObject doc = new BasicDBObject("username", username).append("timestamp", date);
                coll.insert(doc);
                System.out.println(username + " online !");
            }
        } 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

private int UpdateLastActive(String username) {
    int ret = 0;//from   w w  w  .j  av a  2  s .  c o  m
    try {

        MongoClient mongoClient = new MongoClient();
        DB db = mongoClient.getDB("mirc");
        DBCollection coll = db.getCollection("activeUser");
        BasicDBObject query = new BasicDBObject("username", username);

        DBCursor cursor = coll.find(query);

        try {
            if (cursor.hasNext()) {
                DBObject temp = cursor.next();
                java.util.Date date = new java.util.Date();
                temp.put("timestamp", date);
                coll.save(temp);
            } else {
                java.util.Date date = new java.util.Date();
                BasicDBObject doc = new BasicDBObject("username", username).append("timestamp", date);
                coll.insert(doc);
                System.out.println(username + " online !");
            }
        } finally {
            cursor.close();
        }

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