Insert into mongodb - Java Big Data

Java examples for Big Data:MongoDB

Description

Insert into mongodb

Demo Code


import com.mongodb.BasicDBObject;
import com.mongodb.BulkWriteOperation;
import com.mongodb.Cursor;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.DBObject;
import com.mongodb.MongoClient;
import java.util.List;
import java.util.Set;
import javax.swing.JOptionPane;
import java.sql.*;


public class InsertTime extends javax.swing.JFrame {


    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        MongoClient mongo = null;//from www . java2s  .  c  om
        DBCursor cursor = null;
        DBCollection coll = null;
        DB db = null;
        BasicDBObject query = new BasicDBObject();
        try {
            mongo = new MongoClient("localhost", 27017);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        db = mongo.getDB("mydb");
        coll = db.getCollection("insert_tab");
        if (coll == null || db == null || mongo == null) {
            JOptionPane.showMessageDialog(null, "collection not found");
            return;
        } else {
            query.put("field1", jTextField1.getText());
            query.put("field2", jTextField2.getText());
            query.put("field3", jTextField3.getText());
            long timeNow = System.nanoTime();
            coll.insert(query);
            long AfterNow = System.nanoTime() - timeNow;
            //System.out.println(""+AfterNow);
            jTextField4.setText("" + AfterNow);
        }
    }



}

Related Tutorials