List of usage examples for com.mongodb MongoClient getDB
@Deprecated public DB getDB(final String dbName)
From source file:DeleteUser.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("application/json"); response.setCharacterEncoding("utf-8"); response.setHeader("Access-Control-Allow-Origin", "*"); try {/*from w ww. j a va2 s .c om*/ out = response.getWriter(); String id = request.getParameter("id"); System.out.println("id received : " + id); 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 users selected successfully"); //read example try { BasicDBObject query = new BasicDBObject("_id", new ObjectId(id)); coll.findAndRemove(query); success = new JSONObject(); success.put("message", "document deleted"); out.println(success); } catch (NullPointerException ex) { error = new JSONObject(); error.put("message", "No result found"); out.println(error); } System.out.println("Document deleted successfully"); } catch (Exception e) { exception = new JSONObject(); try { exception.put("message", "exception"); out.println(exception); } catch (JSONException ex) { Logger.getLogger(DeleteUser.class.getName()).log(Level.SEVERE, null, ex); } System.err.println(e.getClass().getName() + ": " + e.getMessage()); e.printStackTrace(); } }
From source file:Vehicle_exit.java
public void fetch() { try {/*from w ww . j a v a2 s . co m*/ 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 w w w .j a v a2 s .c o m 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();/*from w w w.ja v a2s. co m*/ int i = 1; try { 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:FollowingCnt.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from w w w .j a va 2 s.com * * @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"); PrintWriter out = response.getWriter(); String username = request.getParameter("username"); try { MongoClient mongo = new MongoClient("localhost", 27017); DB db = mongo.getDB("test"); System.out.println("Connection established"); DBCollection User = db.getCollection("User"); BasicDBObject query = new BasicDBObject("username", username); DBCursor curssc = User.find(query); ArrayList<String> e = new ArrayList(); e = (ArrayList<String>) curssc.next().get("following_username"); out.println(e.size()); } catch (Exception e) { System.err.println(e.getClass().getName() + ": " + e.getMessage()); } }
From source file:gMIRC_handler.java
/** * Add a user to a channel (can check if the channel is a new channel) * * @param Username/*from w w w. java2 s.c o m*/ * @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//from www . ja va 2s . com * @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 a va 2 s.co m * @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/* www . j ava 2s . c o m*/ * @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 ava2s. c om 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; }