Example usage for com.mongodb BasicDBObject append

List of usage examples for com.mongodb BasicDBObject append

Introduction

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

Prototype

@Override
public BasicDBObject append(final String key, final Object val) 

Source Link

Document

Add a key/value pair to this object

Usage

From source file:Veehicle_entry.java

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

    try {/*from w w w.j ava 2  s . c om*/

        String name, vehno, mobno;

        Random rand = new Random();
        token = rand.nextInt(10000);

        name = Name_field.getText();

        mobno = mobno_field.getText();

        vehno = vehno_strt1.getSelectedItem().toString() + "-" + vehno_strt2.getText() + "-"
                + vehno_strt3.getSelectedItem().toString() + "-" + vehno_strt4.getText();

        //System.out.println(vehno);

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

        DBCollection collection1 = db.getCollection("vehicle_count");
        DBCollection collection2 = db.getCollection("statistics");

        if (veh_count == 2) {

            vehno = vehno + "W2";

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

            while (cursorx.hasNext()) {

                BasicDBObject objx = (BasicDBObject) cursorx.next();

                String temp;
                temp = objx.getString("vehno");
                if (temp.equals(vehno)) {
                    JOptionPane.showMessageDialog(null, "vehicle number already exits!\n Entry Rejected!");
                    exists = 1;
                    setVisible(false);
                    new Veehicle_entry(veh_count).setVisible(true);

                }
            }

            if (exists == 0) {
                int count_bike;

                BasicDBObject doc = new BasicDBObject("_id", token).append("name", name)
                        .append("mobileno", mobno).append("vehno", vehno).append("intime", time_mills);

                validator = validate(collection, doc);
                if (validator == 1) {
                    //Updating vehicle count....
                    BasicDBObject query = new BasicDBObject();
                    BasicDBObject field = new BasicDBObject();
                    field.put("wheeler_2", 1);
                    DBCursor cursor = collection1.find(query, field);

                    BasicDBObject obj = (BasicDBObject) cursor.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);

                    //updating session...
                    int cnt_bike;
                    BasicDBObject query2 = new BasicDBObject();
                    BasicDBObject field2 = new BasicDBObject();
                    field2.put("2wheeler_count", 1);
                    DBCursor cursor2 = collection2.find(query2, field2);

                    BasicDBObject obj2 = (BasicDBObject) cursor2.next();
                    cnt_bike = obj2.getInt("2wheeler_count");
                    System.out.println(cnt_bike);

                    BasicDBObject searchUpdate2 = new BasicDBObject();
                    searchUpdate2.append("2wheeler_count", cnt_bike);

                    BasicDBObject UpdateObject2 = new BasicDBObject();
                    UpdateObject2.append("$set", new BasicDBObject("2wheeler_count", cnt_bike + 1));

                    collection2.update(searchUpdate2, UpdateObject2);

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

        else if (veh_count == 4) {

            vehno = vehno + "W4";

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

            while (cursorx.hasNext()) {

                BasicDBObject objx = (BasicDBObject) cursorx.next();

                String temp;
                temp = objx.getString("vehno");
                if (temp.equals(vehno)) {
                    JOptionPane.showMessageDialog(null, "vehicle number already exits!\n Entry Rejected!");
                    exists = 1;
                    setVisible(false);
                    new Veehicle_entry(veh_count).setVisible(true);

                }
            }

            if (exists == 0) {
                int count_car;

                BasicDBObject doc = new BasicDBObject("_id", token).append("name", name)
                        .append("mobileno", mobno).append("vehno", vehno).append("intime", time_mills);

                validator = validate(collection, doc);
                if (validator == 1) {

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

                    BasicDBObject obj = (BasicDBObject) cursor.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);

                    //updating session...
                    int cnt_car;
                    BasicDBObject query2 = new BasicDBObject();
                    BasicDBObject field2 = new BasicDBObject();
                    field2.put("4wheeler_count", 1);
                    DBCursor cursor2 = collection2.find(query2, field2);

                    BasicDBObject obj2 = (BasicDBObject) cursor2.next();
                    cnt_car = obj2.getInt("4wheeler_count");
                    System.out.println(cnt_car);

                    BasicDBObject searchUpdate2 = new BasicDBObject();
                    searchUpdate2.append("4wheeler_count", cnt_car);

                    BasicDBObject UpdateObject2 = new BasicDBObject();
                    UpdateObject2.append("$set", new BasicDBObject("4wheeler_count", cnt_car + 1));

                    collection2.update(searchUpdate2, UpdateObject2);

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

        else {
            vehno = vehno + "W6";

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

            while (cursorx.hasNext()) {

                BasicDBObject objx = (BasicDBObject) cursorx.next();

                String temp;
                temp = objx.getString("vehno");
                if (temp.equals(vehno)) {
                    JOptionPane.showMessageDialog(null, "vehicle number already exits!\n Entry Rejected!");
                    exists = 1;
                    setVisible(false);
                    new Veehicle_entry(veh_count).setVisible(true);

                }
            }
            if (exists == 0) {

                int count_other;

                BasicDBObject doc = new BasicDBObject("_id", token).append("name", name)
                        .append("mobileno", mobno).append("vehno", vehno).append("intime", time_mills);

                validator = validate(collection, doc);
                if (validator == 1) {

                    System.out.println("Came 1");
                    BasicDBObject query = new BasicDBObject();
                    BasicDBObject field = new BasicDBObject();
                    field.put("other", 1);
                    DBCursor cursor = collection1.find(query, field);

                    BasicDBObject obj = (BasicDBObject) cursor.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);

                    //updating session...
                    int cnt_other;
                    BasicDBObject query2 = new BasicDBObject();
                    BasicDBObject field2 = new BasicDBObject();
                    field2.put("other_count", 1);
                    DBCursor cursor2 = collection2.find(query2, field2);

                    BasicDBObject obj2 = (BasicDBObject) cursor2.next();
                    cnt_other = obj2.getInt("other_count");
                    System.out.println(cnt_other);

                    BasicDBObject searchUpdate2 = new BasicDBObject();
                    searchUpdate2.append("other_count", cnt_other);

                    BasicDBObject UpdateObject2 = new BasicDBObject();
                    UpdateObject2.append("$set", new BasicDBObject("other_count", cnt_other + 1));

                    collection2.update(searchUpdate2, UpdateObject2);

                    setVisible(false);
                    new Entry_OR_Exit().setVisible(true);
                }
            }
        }
    } catch (Exception e) {

    }

}

From source file:DeleteRecord.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    // TODO add your handling code here:
    MongoClient mongoClient;//  w  w  w  . ja v  a2 s .c o m
    try {
        mongoClient = new MongoClient("localhost", 27017);
        DB db = mongoClient.getDB("Classic_Hangman");
        System.out.println("Connected to database successfully!!");
        BasicDBObject doc = new BasicDBObject();
        DBCollection coll = db.getCollection("movies");
        DBCollection coll1 = db.getCollection("counter");
        System.out.println("Collection selected successfully");
        DBObject c = coll1.findOne();
        int count = ((Number) c.get("count")).intValue();
        int del_id = Integer.parseInt(jTextField1.getText());
        doc.put("id", del_id);
        coll.remove(doc);
        for (int i = del_id + 1; i <= count; i++) {
            BasicDBObject newDocument = new BasicDBObject();
            newDocument.append("$set", new BasicDBObject().append("id", i - 1));
            BasicDBObject searchQuery = new BasicDBObject().append("id", i);
            coll.update(searchQuery, newDocument);
        }
        count--;
        coll1.remove(new BasicDBObject());
        BasicDBObject doc1 = new BasicDBObject("count", count);
        coll1.insert(doc1);
        JOptionPane.showMessageDialog(this, "Record deleted!");
    } catch (UnknownHostException ex) {
        Logger.getLogger(AdminLogin.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:Modificar.java

public BasicDBObject createBDBOfromArrays(Object[] columnnames, Object[] data) {
    BasicDBObject obj = new BasicDBObject();
    if (data.length == columnnames.length) {
        for (int i = 0; i < data.length; i++) {
            obj.append((String) columnnames[i], data[i]);

        }// w ww .j a  v a 2 s  . c  o  m
    }
    return obj;
}

From source file:Modificar.java

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    //String e = EdadT.getText();
    //int i = Integer.parseInt(EdadT.getText());

    if (Nombre.getText().equals("") || ApPaterno.getText().equals("") || ApMaterno.getText().equals("")
            || EdadT.getText().equals("") || matricula.getText().equals("") || Carrera.getText().equals("")) {
        JOptionPane.showMessageDialog(null, "Faltan campos por llenar");
    } else {//from   w ww .  jav  a  2 s . c o m
        //            document.put("Nombre", ""+ NombreT.getText() +"");
        //            document.put("ApPaterno", ""+ ApPaterno.getText() +"");
        //            document.put("ApMaterno", ""+ ApMaterno.getText() +"");
        //            document.put("FechaNac", (String) dia.getSelectedItem() + "-" + mes.getSelectedItem() + "-" + anio.getSelectedItem());
        //            document.put("Edad", Integer.parseInt(EdadT.getText()));
        //            document.put("Sexo", ""+ sexo.getSelectedItem() +"");
        //            document.put("Matricula", ""+ matricula.getText() +"");
        //            document.put("Semestre", Integer.parseInt(""+ semestre.getSelectedItem()));
        //            document.put("Carrera", ""+ Carrera.getText() +"");
        //            collection.insert(document);

        Object[] obj = new Object[] { NombreT.getText(), ApPaterno.getText(), ApMaterno.getText(),
                dia.getSelectedItem() + "-" + mes.getSelectedItem() + "-" + anio.getSelectedItem(),
                Integer.parseInt(EdadT.getText()), sexo.getSelectedItem(), matricula.getText(),
                Integer.parseInt("" + semestre.getSelectedItem()), Carrera.getText() };
        BasicDBObject updateQuery = new BasicDBObject();
        updateQuery.append("$set", createBDBOfromArrays(columnnames, obj));
        BasicDBObject searchQuery = new BasicDBObject();
        searchQuery.put("$and", createListBDBOfromArrays(this.columnnames, this.data));
        System.out.println(updateQuery.toString());
        System.out.println(searchQuery.toString());
        System.err.println(collection.update(searchQuery, updateQuery).getError());
        javax.swing.JOptionPane.showMessageDialog(this, "Se ha actualizado exitosamente el alumno");
        PanModificar newFrame = new PanModificar();
        newFrame.setVisible(true);
        this.dispose();
    }

    // TODO add your handling code here:
}

From source file:loc.java

public void loc() {
    MongoClient mongoClient = new MongoClient("localhost", 27017);
    DB dbBusiness = mongoClient.getDB("business");
    DBCollection collBusiness = dbBusiness.getCollection("business");
    List<DBObject> categoryIdList = new ArrayList<DBObject>();
    DBCursor cursorForBusiness = collBusiness.find();
    while (cursorForBusiness.hasNext()) {
        categoryIdList.add(cursorForBusiness.next());
    }/*w w  w  . j  av a 2s.c om*/
    for (DBObject s : categoryIdList) {
        System.out.println(s.get("business_id").toString());
        System.out.println(s.get("longitude").toString());
        System.out.println(s.get("latitude").toString());

        BasicDBObject newDocument = new BasicDBObject();
        BasicDBList addFields = new BasicDBList();
        addFields.add(s.get("longitude").toString());
        addFields.add(s.get("latitude").toString());
        newDocument.append("$set", new BasicDBObject().append("loc", addFields));

        BasicDBObject searchQuery = new BasicDBObject().append("business_id", s.get("business_id").toString());

        collBusiness.update(searchQuery, newDocument);
    }

}

From source file:Vehicle_type_check.java

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

    try {/*from   w  w w.j av  a2  s .  c  om*/
        int count_bike;
        System.out.println("Hello_End");
        MongoClient mc = new MongoClient("localhost", 27017);
        DB db = mc.getDB("parking_system");
        DBCollection collection = db.getCollection("vehicle_count");

        BasicDBObject query = new BasicDBObject();
        BasicDBObject field = new BasicDBObject();
        field.put("wheeler_2", 1);
        DBCursor cursor = collection.find(query, field);

        BasicDBObject obj = (BasicDBObject) cursor.next();
        count_bike = obj.getInt("wheeler_2");
        System.out.println(count_bike);

        if (count_bike < 50) {

            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);

            setVisible(false);
            new Veehicle_entry().setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null, "No Available Space For Parking 2 Wheelers");
        }

    } catch (Exception e) {

    }
}

From source file:Vehicle_type_check.java

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

    try {/*w w w .j a  va2s  .c o m*/
        int count_other;

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

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

        BasicDBObject obj = (BasicDBObject) cursor.next();
        count_other = obj.getInt("other");
        System.out.println(count_other);

        if (count_other < 50) {

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

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

            collection.update(searchUpdate, UpdateObject);

            setVisible(false);
            new Veehicle_entry().setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null, "No Available Space For Parking Trucks");
        }

    } catch (Exception e) {

    }

}

From source file:Vehicle_type_check.java

private void car_buttonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_car_buttonActionPerformed
    // TODO add your handling code here:
    try {/*from  w  w  w .ja  va2s. com*/
        int count_car;

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

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

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

        if (count_car < 50) {

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

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

            collection.update(searchUpdate, UpdateObject);

            setVisible(false);
            new Veehicle_entry().setVisible(true);
        } else {
            JOptionPane.showMessageDialog(null, "No Available Space For Parking 2 Wheelers");
        }

    } catch (Exception e) {

    }

}

From source file:Add_cust.java

@Override
/* Check The code below for insertion of values */
public void actionPerformed(ActionEvent ae) {
    // TODO Auto-generated method stub
    add(jLabel9);/*from www. ja  v a 2s  . c om*/
    String History = jComboBox3.getSelectedItem().toString();
    if (ae.getSource() == jButton1) {
        try {
            MongoClient mongo = new MongoClient("localhost", 27017);
            DB db = mongo.getDB("Gym");
            DBCollection Gym_Collection = db.getCollection("Customers");
            DBCollection Allot = db.getCollection("Batch"); //Declaration of collection for Batch
            DBCursor cursor = Allot.find();
            float Slots, Morning, Evening, Night;
            cursor.next();
            Slots = Float.parseFloat(cursor.curr().get("Slots").toString());
            Morning = Float.parseFloat(cursor.curr().get("Morning").toString());
            Evening = Float.parseFloat(cursor.curr().get("Evening").toString());
            Night = Float.parseFloat(cursor.curr().get("Night").toString());
            String first_name = jTextField1.getText();
            String last_name = jTextField2.getText();
            float weight = Float.parseFloat(jTextField6.getText().toString());
            float height = Float.parseFloat(jTextField5.getText().toString());
            String address = jTextArea1.getText();
            int age = Integer.parseInt(jComboBox1.getSelectedItem().toString());
            String Subs = jComboBox2.getSelectedItem().toString();

            String Batches = jComboBox5.getSelectedItem().toString();
            String phone_number = jTextField3.getText().toString();
            if (!isAlpha(first_name) || !isAlpha(last_name)) {
                JOptionPane.showMessageDialog(this, "Enter a valid name");
                jTextField1.setText("");
                jTextField2.setText("");
            } else if (weight > 150 || weight < 40) {
                JOptionPane.showMessageDialog(this, "Please add weight between 40 and 150");
                jTextField6.setText("");
            } else if (height > 250 || height < 50) {
                JOptionPane.showMessageDialog(this, "Please add height between 100 and 250");
                jTextField5.setText("");
            } else if (phone_number.length() < 8 || phone_number.length() > 11) {
                JOptionPane.showMessageDialog(this, "Enter a valid Phone no.");
                jTextField3.setText("");
            } else {
                int random_1, random_2, sub_sum = 0, days = 0;
                if (Subs.equals("1 Month")) {
                    sub_sum = 1000;
                    days = 30;
                } else if (Subs.equals("2 Months")) {
                    sub_sum = 1500;
                    days = 60;
                } else if (Subs.equals("3 Months")) {
                    sub_sum = 2250;
                    days = 90;
                } else if (Subs.equals("1 YEAR")) {
                    sub_sum = 6000;
                    days = 360;
                }
                int flag = 0, flag1 = 0, flag2 = 0, flag3 = 0;
                if (Batches.equals("Morning")) {
                    Morning += 1;
                    flag1 = 1;
                } else if (Batches.equals("Evening")) {
                    Evening += 1;
                    flag2 = 1;
                } else if (Batches.equals("Night")) {
                    Night += 1;
                    flag3 = 1;
                }
                if (Morning > Slots || Evening > Slots || Night > Slots) {
                    if (flag1 == 1) {
                        Morning -= 1;
                    } else if (flag2 == 1) {
                        Evening -= 1;
                    } else if (flag3 == 1) {
                        Night -= 1;
                    }
                    flag = 1;
                }
                if (flag == 1) {
                    float temp1, temp2;
                    if (flag1 == 1) {
                        temp1 = Slots - Evening;
                        temp2 = Slots - Night;
                        JOptionPane.showMessageDialog(this, "This time slot slot is full. Evening has " + temp1
                                + " Slots. Night has " + temp2 + " Slots.");
                    } else if (flag2 == 1) {
                        temp1 = Slots - Morning;
                        temp2 = Slots - Night;
                        JOptionPane.showMessageDialog(this, "This time slot slot is full. Morning has " + temp1
                                + " Slots. Night has " + temp2 + " Slots.");
                    } else {
                        temp1 = Slots - Morning;
                        temp2 = Slots - Evening;
                        JOptionPane.showMessageDialog(this, "This time slot slot is full. Morning has " + temp1
                                + " Slots. Evening has " + temp2 + " Slots.");
                    }
                } else {
                    do {
                        random_1 = (int) Math.floor(Math.random() * 360);
                        random_2 = (int) Math.floor(Math.random() * 360);
                    } while (random_1 >= random_2 || days <= random_2);
                    BasicDBObject doc = new BasicDBObject("First_Name", first_name.toLowerCase())
                            .append("Last_Name", last_name.toLowerCase()).append("Phone_Number", phone_number)
                            .append("Age", age).append("Height", height).append("History", History)
                            .append("Subscribe", days).append("Batch", Batches).append("Weight", weight)
                            .append("Attended", random_1).append("Total", random_2).append("Fees", sub_sum)
                            .append("Address", address);
                    Gym_Collection.insert(doc);
                    jTextField1.setText("");
                    jTextField2.setText("");
                    jTextField3.setText("");
                    jTextField5.setText("");
                    jTextField6.setText("");
                    jTextArea1.setText("");

                    BasicDBObject newDocument = new BasicDBObject();
                    newDocument.append("$set", new BasicDBObject().append("Morning", Morning)
                            .append("Evening", Evening).append("Night", Night));

                    BasicDBObject searchQuery = new BasicDBObject().append("Slots", Slots);

                    Allot.update(searchQuery, newDocument);

                    this.setVisible(false);
                    new work(History, first_name.toLowerCase(), last_name.toLowerCase());
                    this.setVisible(false);
                    new work().setVisible(true);
                }
            }
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NumberFormatException e) {// To validate for data types
            JOptionPane.showMessageDialog(this, "Please Check the Data types");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField5.setText("");
            jTextField6.setText("");
            jTextArea1.setText("");
        }
    }

    else if (ae.getSource() == jButton2) {
        this.setVisible(false);
        new Receptionist().setVisible(true);
    }
}

From source file:Add_train.java

@Override
/* Check The code below for insertion of values */
public void actionPerformed(ActionEvent ae) {
    // TODO Auto-generated method stub
    add(jLabel9);//from  w  w  w. j  a v  a2  s  .co m
    jLabel9.setText("");
    if (ae.getSource() == jButton1) {
        try {
            MongoClient mongo = new MongoClient("localhost", 27017);
            DB db = mongo.getDB("Gym");
            DBCollection Allot = db.getCollection("Batch_Trainer");
            DBCursor cursor1 = Allot.find();
            DBCollection Gym_Collection = db.getCollection("trainer");
            String first_name = jTextField1.getText();
            String last_name = jTextField2.getText();
            String password = jPassword1.getText().toString();
            String dup = jPassword2.getText().toString();
            String username = jTextField5.getText().toString();
            String address = jTextArea1.getText();

            String Batches = jComboBox5.getSelectedItem().toString();
            float Slots, Morning, Evening, Night;
            cursor1.next();
            Slots = Float.parseFloat(cursor1.curr().get("Slots").toString());
            Morning = Float.parseFloat(cursor1.curr().get("Morning").toString());
            Evening = Float.parseFloat(cursor1.curr().get("Evening").toString());
            Night = Float.parseFloat(cursor1.curr().get("Night").toString());
            int age = Integer.parseInt(jComboBox1.getSelectedItem().toString());
            String phone_number = jTextField3.getText().toString();
            DBCursor cursor = Gym_Collection.find();
            int i = 0;
            while (cursor.hasNext()) {
                cursor.next();
                if (username.equals(cursor.curr().get("Username").toString())) {
                    i = 1;
                }
            }
            if (i == 1) {
                JOptionPane.showMessageDialog(this, "Username has been taken !!!");
                jTextField5.setText("");
            } else if (!isAlpha(first_name) || !isAlpha(last_name)) {
                JOptionPane.showMessageDialog(this, "Enter a valid name");
                jTextField1.setText("");
                jTextField2.setText("");
            } else if (phone_number.length() < 8 || phone_number.length() > 10) {
                JOptionPane.showMessageDialog(this, "Enter a valid Phone no.");
                jTextField3.setText("");
            } else if (!password.equals(dup)) {
                JOptionPane.showMessageDialog(this, "Passwords didn't match");
                jPassword1.setText("");
                jPassword2.setText("");
            } else {

                int flag = 0, flag1 = 0, flag2 = 0, flag3 = 0;
                if (Batches.equals("Morning")) {
                    Morning += 1;
                    flag1 = 1;
                } else if (Batches.equals("Evening")) {
                    Evening += 1;
                    flag2 = 1;
                } else if (Batches.equals("Night")) {
                    Night += 1;
                    flag3 = 1;
                }
                if (Morning > Slots || Evening > Slots || Night > Slots) {
                    if (flag1 == 1) {
                        Morning -= 1;
                    } else if (flag2 == 1) {
                        Evening -= 1;
                    } else if (flag3 == 1) {
                        Night -= 1;
                    }
                    flag = 1;
                }
                if (flag == 1) {
                    float temp1, temp2;
                    if (flag1 == 1) {
                        temp1 = Slots - Evening;
                        temp2 = Slots - Night;
                        JOptionPane.showMessageDialog(this, "This time slot slot is full. Evening has " + temp1
                                + " Slots. Night has " + temp2 + " Slots.");
                    } else if (flag2 == 1) {
                        temp1 = Slots - Morning;
                        temp2 = Slots - Night;
                        JOptionPane.showMessageDialog(this, "This time slot slot is full. Morning has " + temp1
                                + " Slots. Night has " + temp2 + " Slots.");
                    } else {
                        temp1 = Slots - Morning;
                        temp2 = Slots - Evening;
                        JOptionPane.showMessageDialog(this, "This time slot slot is full. Morning has " + temp1
                                + " Slots. Evening has " + temp2 + " Slots.");
                    }
                } else {

                    BasicDBObject doc = new BasicDBObject("First_Name", first_name.toLowerCase())
                            .append("Last_Name", last_name.toLowerCase()).append("Batch", Batches)
                            .append("Phone_Number", phone_number).append("Age", age)
                            .append("Username", username).append("Password", password)
                            .append("Address", address);
                    Gym_Collection.insert(doc);
                    jTextField1.setText("");
                    jTextField2.setText("");
                    jTextField3.setText("");
                    jTextField5.setText("");
                    jPassword1.setText("");
                    jPassword2.setText("");
                    jTextArea1.setText("");
                    jLabel9.setFont(new java.awt.Font("SansSerif", 3, 16)); // NOI18N
                    jLabel9.setForeground(new java.awt.Color(204, 255, 204));
                    jLabel9.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
                    jLabel9.setText("Successfully Added");
                    jLabel9.setBounds(200, 10, 200, 40);

                    BasicDBObject newDocument = new BasicDBObject();
                    newDocument.append("$set", new BasicDBObject().append("Morning", Morning)
                            .append("Evening", Evening).append("Night", Night));

                    BasicDBObject searchQuery = new BasicDBObject().append("Slots", Slots);

                    Allot.update(searchQuery, newDocument);

                    setVisible(true);
                }
            }
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, "Please Check the Data types");
            jTextField1.setText("");
            jTextField2.setText("");
            jTextField3.setText("");
            jTextField5.setText("");
            jPassword1.setText("");
            jPassword2.setText("");
            jTextArea1.setText("");
        }
    }

    else if (ae.getSource() == jButton2) {
        this.setVisible(false);
        new Admin().setVisible(true);
    }
}