List of usage examples for com.mongodb MongoClient MongoClient
public MongoClient(final MongoClientURI uri, final MongoDriverInformation mongoDriverInformation)
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 av a 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:MenuDefTyp.java
public static void getTypes() { MongoClient mongoClient = new MongoClient("localhost", 27017); MongoDatabase db = mongoClient.getDatabase("database"); MongoCollection<Document> elexirCollection = db.getCollection("test"); FindIterable<Document> results = elexirCollection .find(new BasicDBObject("Objects", new BasicDBObject("$gt", ".0001"))); //FindIterable<Document> iter = elexirCollection.find(new BasicDBObject("derivProb", 2)); for (Document doc : results) { // do something with each result List<String> conv = (List<String>) doc.get("Types"); String[] convArr = new String[conv.size()]; convArr = conv.toArray(convArr); for (String s : convArr) TypTextArea.append(s + "\n"); }//from w w w . jav a 2 s . c o m }
From source file:MenuDefPred.java
public static void getPreds() { MongoClient mongoClient = new MongoClient("localhost", 27017); MongoDatabase db = mongoClient.getDatabase("database"); MongoCollection<Document> elexirCollection = db.getCollection("test"); FindIterable<Document> results = elexirCollection .find(new BasicDBObject("Types", new BasicDBObject("$gt", "0"))); //FindIterable<Document> iter = elexirCollection.find(new BasicDBObject("derivProb", 2)); for (Document doc : results) { List<String> conv = (List<String>) doc.get("Predicates"); String[] convArr = new String[conv.size()]; convArr = conv.toArray(convArr); for (String s : convArr) PredTextArea.append(s + "\n"); }/* w w w. j a v a2 s . com*/ }
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;/*from w ww. j av a 2 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:AppletECF.java
public String salvarCupomFiscal() { MongoClient mongo = null;// www. j ava2s. c o m try { mongo = new MongoClient("192.168.252.10", 27017); } catch (Exception ex) { } try { if (mongo == null) { // se nao achou o mongo db no servidor salva local mongo = new MongoClient("localhost", 27017); } } catch (Exception ex) { return "Nao foi possivel conectar a base local! Verifique se o MongoDB esta instalado no seu computador. Erro: " + ex.getMessage(); } DB db = mongo.getDB("cupons_db"); DBCollection cupons = db.getCollection("cupons"); cupomFiscal.put("itens", new ArrayList<BasicDBObject>()); cupons.insert(cupomFiscal); return ""; }
From source file:Welcome.java
public void setListVal() { int i = 1;/*w w w . j ava2 s . c o m*/ MongoClient client = new MongoClient("localhost", 27017); MongoDatabase db = client.getDatabase("ExpenseManager"); MongoCollection<Document> reg = db.getCollection("Registration"); FindIterable<Document> find = reg.find(new Document("unm", unm)); Document doc = find.first(); DefaultListModel model; model = new DefaultListModel(); while (i <= 5) { if (!doc.containsKey("Account" + i)) { i++; } else { while (doc.containsKey("Account" + i)) { String s = (String) doc.get("Account" + i); s = s.concat(" "); s = s.concat((String) doc.get("Bank" + i)); s = s.concat(" "); s = s.concat((String) doc.get("Money" + i)); model.addElement(s); i++; } } } jList1.setModel(model); }
From source file:Welcome.java
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed String accno = JOptionPane.showInputDialog(this, "Enter Your Account Number"); if (isInteger(accno)) { int value = JOptionPane.showConfirmDialog(this, "Do You Want to add Account No. " + accno); if (value == 0) { String Banks[] = { "", "Allahabad Bank", "Andhra Bank", "Bank of Baroda", "Bank of India", "Bank of Maharashtra", "Canara Bank", "Central Bank of India", "Corporation Bank", "Dena Bank", "Indian Bank", "Indian Overseas Bank", "Oriental Bank of Commerce", "Punjab & Sind Bank", "Punjab National Bank", "Syndicate Bank", "UCO Bank", "Union Bank of India", "United Bank of India", "Vijaya Bank", "Axis Bank", "City Union Bank", "Dhanlaxmi Bank", "Federal Bank", "HDFC Bank", "ICICI Bank", "IDFC Bank", "Karnataka Bank", "IndusInd Bank", "ING Vysya Bank", "Jammu and Kashmir Bank", "Karur Vysya Bank", "Kotak Mahindra Bank", "Yes bank", "Citi Bank", "State Bank of India", "State Bank of Patiala", "State Bank of Mysore", "State Bank of Travancore", "State Bank of Bikaner and Jaipur", "State Bank of Hyderabad", "State Bank Of Saurashtra" };/* w w w. j a v a 2 s . c o m*/ Arrays.sort(Banks); JComboBox jcb = new JComboBox(Banks); jcb.setEditable(true); JOptionPane.showMessageDialog(this, jcb, "Select Your Bank", JOptionPane.QUESTION_MESSAGE); while (jcb.getSelectedItem().equals("")) { JOptionPane.showMessageDialog(this, "Please Select the Bank"); JOptionPane.showMessageDialog(this, jcb, "Select Your Bank", JOptionPane.QUESTION_MESSAGE); } String money = JOptionPane.showInputDialog(this, "Enter the amount present in " + accno + " account"); String Bank = (String) jcb.getSelectedItem(); while (!isInteger(money)) { JOptionPane.showMessageDialog(this, "You entered a Wrong Value. Please Enter Correct Value"); money = JOptionPane.showInputDialog(this, "Enter the amount present in " + accno + " account"); } MongoClient client = new MongoClient("localhost", 27017); DB db; db = client.getDB("ExpenseManager"); DBCollection reg = (DBCollection) db.getCollection("Registration"); DBObject query = new BasicDBObject("unm", unm); DBObject update = new BasicDBObject(); DBCursor find = reg.find(query); while (find.hasNext()) { DBObject next = find.next(); int count = 1; for (int i = 1; i <= 5; i++) { String str1 = "Account" + i; String str2 = "Bank" + i; String str3 = "Money" + i; System.out.println(str1); if (!(next.containsField(str1))) { update.put("$set", new BasicDBObject(str1, accno).append(str2, Bank).append(str3, money)); WriteResult result = reg.update(query, update); setListVal(); break; } } } } } else { JOptionPane.showMessageDialog(this, "You Entered a wrong Number"); } // TODO add your handling code here: }
From source file:Welcome.java
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed /*MongoClient client = new MongoClient("localhost", 27017); DB db;// w w w. ja v a2s .c om db = client.getDB("ExpenseManager"); DBCollection reg = (DBCollection) db.getCollection("Registration"); DBObject query = new BasicDBObject("unm", unm); DBObject update = new BasicDBObject(); update.put("$unset", new BasicDBObject("userId","")); */ int i = 1; int count = 1; MongoClient client = new MongoClient("localhost", 27017); MongoDatabase db = client.getDatabase("ExpenseManager"); MongoCollection<Document> reg = db.getCollection("Registration"); FindIterable<Document> find = reg.find(new Document("unm", unm)); DBObject query = new BasicDBObject("unm", unm); BasicDBObject update = new BasicDBObject(); Map<Integer, Integer> m = new HashMap<Integer, Integer>(); String val[] = new String[6]; val[0] = ""; Document doc = find.first(); while (i <= 5) { if (!doc.containsKey("Account" + i)) { i++; } else { while (doc.containsKey("Account" + i)) { m.put(count, i); val[count] = (String) (doc.get("Account" + i)); i++; count++; } } } JComboBox jcb = new JComboBox(val); jcb.setEditable(true); int msg = JOptionPane.showConfirmDialog(this, "Do You Really Want to remove Your Account?"); if (msg == 0) { JOptionPane.showMessageDialog(this, jcb, "Select Your Account to Remove", JOptionPane.QUESTION_MESSAGE); while (jcb.getSelectedItem().equals("")) { JOptionPane.showMessageDialog(this, "You Haven't chose any Account, Please Choose the Account"); JOptionPane.showMessageDialog(this, jcb, "Select Your Account to Remove", JOptionPane.QUESTION_MESSAGE); } int index = jcb.getSelectedIndex(); int value = m.get(index); String str1 = "Account" + value; String str2 = "Bank" + value; String str3 = "Money" + value; update.put("$unset", new BasicDBObject(str1, "").append(str2, "").append(str3, "")); UpdateResult updateOne = reg.updateOne(new Document("unm", unm), update); setListVal(); } // TODO add your handling code here: }
From source file:Search_trainer.java
@Override public void actionPerformed(ActionEvent ae) { // TODO Auto-generated method stub add(jLabel3);//from www .java2 s.c o m add(jTextField1); add(jButton1); if (ae.getSource() == jComboBox1) { String Variable = jComboBox1.getSelectedItem().toString(); jLabel3.setText(""); jTextField1.setText(""); if (Variable == "All_Trainers") { try { MongoClient mongo = new MongoClient("localhost", 27017); DB db = mongo.getDB("Gym"); DBCollection Gym_Collection = db.getCollection("trainer"); DBCursor cursor1 = Gym_Collection.find(); int i = 0; String[][] s = new String[20][10]; while (cursor1.hasNext()) { cursor1.next(); s[i][0] = cursor1.curr().get("First_Name").toString(); s[i][1] = cursor1.curr().get("Last_Name").toString(); s[i][2] = cursor1.curr().get("Batch").toString(); s[i][3] = cursor1.curr().get("Age").toString(); s[i][4] = cursor1.curr().get("Username").toString(); s[i][5] = cursor1.curr().get("Password").toString(); s[i][6] = cursor1.curr().get("Phone_Number").toString(); s[i][7] = cursor1.curr().get("Address").toString(); i++; } JFrame frame = new JFrame("Resulted Trainers"); frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE); Trainer_Table ne = new Trainer_Table(s); ne.setOpaque(true); frame.setContentPane(ne); frame.pack(); frame.setVisible(true); } catch (UnknownHostException e) { // TODO Auto-generated catch block e.printStackTrace(); } } else { jLabel3.setFont(new java.awt.Font("SansSerif", 3, 14)); // NOI18N jLabel3.setForeground(new java.awt.Color(204, 255, 204)); jLabel3.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT); jLabel3.setText("Enter " + Variable + ": "); jLabel3.setBounds(55, 230, 200, 40); jTextField1.setBounds(267, 230, 200, 40); jButton1.setBackground(new java.awt.Color(51, 0, 51)); jButton1.setFont(new java.awt.Font("SansSerif", 3, 12)); // NOI18N jButton1.setForeground(new java.awt.Color(255, 255, 255)); jButton1.setText("Search Trainer"); jButton1.setBounds(160, 300, 300, 40); setVisible(true); } } else if (ae.getSource() == jButton1) { try { String Variable = jComboBox1.getSelectedItem().toString(); MongoClient mongo = new MongoClient("localhost", 27017); DB db = mongo.getDB("Gym"); DBCollection Gym_Collection = db.getCollection("trainer"); BasicDBObject fields; if (Variable == "First_Name" || Variable == "Last_Name" || Variable == "Username" || Variable == "Batch") { fields = new BasicDBObject(Variable, jTextField1.getText().toString()); } else { fields = new BasicDBObject(Variable, Integer.parseInt(jTextField1.getText().toString())); } DBCursor cursor1 = Gym_Collection.find(fields); int i = 0; String[][] s = new String[20][10]; while (cursor1.hasNext()) { cursor1.next(); s[i][0] = cursor1.curr().get("First_Name").toString(); s[i][1] = cursor1.curr().get("Last_Name").toString(); s[i][2] = cursor1.curr().get("Batch").toString(); s[i][3] = cursor1.curr().get("Age").toString(); s[i][4] = cursor1.curr().get("Username").toString(); s[i][5] = cursor1.curr().get("Password").toString(); s[i][6] = cursor1.curr().get("Phone_Number").toString(); s[i][7] = cursor1.curr().get("Address").toString(); i++; } JFrame frame = new JFrame("Resulted Trainers"); frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE); Trainer_Table ne = new Trainer_Table(s); ne.setOpaque(true); frame.setContentPane(ne); frame.pack(); frame.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(""); } } else if (ae.getSource() == jButton2) { this.setVisible(false); new Admin().setVisible(true); } }
From source file:Slots.java
Slots() { mongoClient = new MongoClient("localhost", 27017); // Now connect to your databases db = mongoClient.getDB("test"); }