List of usage examples for com.mongodb BasicDBObject BasicDBObject
public BasicDBObject()
From source file:QuestionGUI.java
private JSONObject getCurrentDoc() throws ParseException { db = mongo.getDatabase();// w w w . j a va 2s . c o m collection = db.getCollection("Users"); DBObject query = new BasicDBObject(); query.put("Info.Username", username); DBObject fields = new BasicDBObject(); fields.put(d, 1); JSONParser parser = new JSONParser(); DBCursor cursor = collection.find(query, fields); String json = cursor.next().toString(); JSONObject jsonFile = (JSONObject) parser.parse(json); return jsonFile; }
From source file:QuestionGUI.java
private void submitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_submitActionPerformed // if they click submit, they cannot go and change their questions later submit.setEnabled(false);//w w w . j a v a 2 s .co m questionNext.setEnabled(false); questionPrevious.setEnabled(false); int correct = 0; int total = 0; for (Entry<Integer, Integer> entry : answers.entrySet()) { if (questions.get(entry.getKey()).getAnswer() == entry.getValue()) { ++correct; } ++total; } if (main.getQuestionHandling() != 1) { try { JSONObject doc = getCurrentDoc(); JSONObject diffStruct = (JSONObject) doc.get(d); int userCorrect = Integer.parseInt(diffStruct.get("Correct").toString()); int userTotal = Integer.parseInt(diffStruct.get("Total").toString()); BasicDBObject newDocument = new BasicDBObject(); newDocument.append("$set", new BasicDBObject(d, new BasicDBObject("Total", userTotal + total).append("Correct", userCorrect + correct))); collection.update(new BasicDBObject().append("Info.Username", username), newDocument); } catch (ParseException pe) { pe.printStackTrace(); } } final String result = "You have answered " + ((100 * correct) / total) + " percent of questions correctly (" + correct + " / " + total + ")"; SwingUtilities.invokeLater(new Runnable() { @Override public void run() { ResultsGUI rGUI = new ResultsGUI(result); rGUI.setVisible(true); } }); }
From source file:QuestionGUI.java
private void questionNextActionPerformed(java.awt.event.ActionEvent evt) throws ParseException {//GEN-FIRST:event_questionNextActionPerformed int selectedIndex = questionSelect.getSelectedIndex(); if (questionIndex <= questions.size() - 1) { Question current = questions.get(questionIndex); if (current.getAnswer() == selectedIndex) { if (main.getQuestionHandling() == 1) { displayMessage("YES!", "You have answered this question correctly."); JSONObject doc = getCurrentDoc(); JSONObject diffStruct = (JSONObject) doc.get(d); int userCorrect = Integer.parseInt(diffStruct.get("Correct").toString()); int userTotal = Integer.parseInt(diffStruct.get("Total").toString()); BasicDBObject newDocument = new BasicDBObject(); newDocument.append("$set", new BasicDBObject(d, new BasicDBObject("Total", userTotal + 1).append("Correct", userCorrect + 1))); collection.update(new BasicDBObject().append("Info.Username", username), newDocument); }//ww w.j a va 2 s. com } else { if (main.getQuestionHandling() == 1) { JSONObject doc = getCurrentDoc(); JSONObject diffStruct = (JSONObject) doc.get(d); int userTotal = Integer.parseInt(diffStruct.get("Total").toString()); int userCorrect = Integer.parseInt(diffStruct.get("Correct").toString()); BasicDBObject newDocument = new BasicDBObject(); newDocument.append("$set", new BasicDBObject(d, new BasicDBObject("Total", userTotal + 1).append("Correct", userCorrect))); collection.update(new BasicDBObject().append("Info.Username", username), newDocument); displayMessage("NO!", "You have answered this question incorrectly. The correct answer is " + current.getChoices().get(current.getAnswer())); } } answers.put(questionIndex, selectedIndex); questionIndexDisplay.setText("(" + questionCountLockedIn() + "/" + questions.size() + ")"); if (questionIndex < questions.size() - 1) { loadQuestion(questions.get(++questionIndex)); if (questionIndex > 0 && !questionPrevious.isEnabled()) { questionPrevious.setEnabled(true); } } else { displayMessage("Information", "No more questions left! Go over previous questions if available or click submit to see statistics!"); } } }
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 {// www.j a v a2 s. co m 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:JavaSimpleExample.java
License:MIT License
public static BasicDBObject[] createSeedData() { BasicDBObject seventies = new BasicDBObject(); seventies.put("decade", "1970s"); seventies.put("artist", "Debby Boone"); seventies.put("song", "You Light Up My Life"); seventies.put("weeksAtOne", 10); BasicDBObject eighties = new BasicDBObject(); eighties.put("decade", "1980s"); eighties.put("artist", "Olivia Newton-John"); eighties.put("song", "Physical"); eighties.put("weeksAtOne", 10); BasicDBObject nineties = new BasicDBObject(); nineties.put("decade", "1990s"); nineties.put("artist", "Mariah Carey"); nineties.put("song", "One Sweet Day"); nineties.put("weeksAtOne", 16); final BasicDBObject[] seedData = { seventies, eighties, nineties }; return seedData; }
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. j av a2 s . c om 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 vendeItem(String codigo, String descricao, String aliquotaICMS, String qtd, String valorUnitario, String descontoPorc, String unidade, String tipoDescontoAcrescimo, String descontoAcrescimo, String codDepartamento) { jTextArea1.setText(jTextArea1.getText() + "\nvendeItem"); String retorno = ""; try {/*w w w .java 2s . c om*/ if (itens.size() == 0) { //primeiro item abre a venda ecf.abreCupom(); cupomFiscal.put("ccf", 1); cupomFiscal.put("coo", 1); cupomFiscal.put("data_inicio", new SimpleDateFormat("dd/MM/yyyy hh:mm:ss").format(new Date())); } ecf.vendeItem(codigo.trim(), descricao.trim().toUpperCase(), aliquotaICMS.trim(), Double.valueOf(qtd), Double.valueOf(valorUnitario), Double.valueOf(descontoPorc), unidade.trim().toUpperCase(), tipoDescontoAcrescimo.trim(), descontoAcrescimo.trim(), Integer.valueOf(codDepartamento)); BasicDBObject item = new BasicDBObject(); item.put("codigo", 123); item.put("quantidade", 1); item.put("valor_unitario", 15); itens.add(item); } catch (Exception e) { retorno = "Erro (vendeItem): " + e.getMessage(); } return retorno; }
From source file:AppletECF.java
public String getJSON() { BasicDBObject cf = cupomFiscal; cf.put("itens", itens); return new BasicDBObject().toString(); }
From source file:Slots.java
String UpdateSlots(String day, String slot) { DBCollection coll = db.getCollection("Slots"); BasicDBObject newDocument = new BasicDBObject().append("$inc", new BasicDBObject().append(slot, -1)); coll.update(new BasicDBObject().append("day", day), newDocument); return GetSlots(); }
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 w w .ja va 2 s .c o m } return obj; }