Query mongodb DBCollection - Java Big Data

Java examples for Big Data:MongoDB

Description

Query mongodb DBCollection

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 Query extends javax.swing.JFrame {

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        DBCursor cursor = null;/*from w w  w. ja va  2s . co  m*/
        MongoClient mongoClient = null;
        try {
            mongoClient = new MongoClient("localhost", 27017); //initialize connection
            DB db = mongoClient.getDB("mydb"); //connect to database, if no authentication then this will work
            DBCollection coll = null;
            if (jRadioButton1.isSelected() == true) {
                coll = db.getCollection("test_tab1");//from database object, get collection with date
            }
            if (jRadioButton2.isSelected() == true) {
                coll = db.getCollection("test_tab2");//from database object, get collection with date
            }
            if (jRadioButton3.isSelected() == true) {
                coll = db.getCollection("test_tab3");//from database object, get collection with date
            }
            BasicDBObject query = new BasicDBObject();
            long timeNow = System.nanoTime();
            cursor = coll.find();
            long timeAfter = System.nanoTime();
            if (cursor.count() > 0) {
                DBObject currentRecord = cursor.next();
                System.out.println("Records fetched");
            } else {
                // JOptionPane.showMessageDialog(null,"Record not found");
                System.out.println("Record not found!");
            }
            long actual = timeAfter - timeNow;
            jTextField2.setText("" + actual);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
        } finally {
            cursor.close();
            mongoClient.close();
        }
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Connection formed successfully");
            String myUrl = "jdbc:mysql://localhost/mydb";
            String pwd = "arijit";
            Connection con = DriverManager
                    .getConnection(myUrl, "root", pwd);
            Statement st = con.createStatement();
            String query = "";
            if (jRadioButton1.isSelected() == true) {
                query = "select * from test_tab1"; //make this query parameterized later
            }
            if (jRadioButton2.isSelected() == true) {
                query = "select * from test_tab2"; //make this query parameterized later
            }
            if (jRadioButton3.isSelected() == true) {
                query = "select * from test_tab3 limit 10000"; //make this query parameterized later
            }
            long timeNow = System.nanoTime();
            ResultSet rs = st.executeQuery(query);
            long timeAfter = System.nanoTime() - timeNow;
            st.close();
            jTextField1.setText("" + timeAfter);
        } catch (Exception e) {
            System.out.println(e);
        } 
    }

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        DBCursor cursor = null;
        MongoClient mongoClient = null;

        try {
            mongoClient = new MongoClient("localhost", 27017); //initialize connection
            DB db = mongoClient.getDB("mydb"); //connect to database, if no authentication then this will work
            DBCollection coll = null;
            if (jRadioButton1.isSelected() == true) {
                coll = db.getCollection("test_tab1");//from database object, get collection with date
            }
            if (jRadioButton2.isSelected() == true) {
                coll = db.getCollection("test_tab2");//from database object, get collection with date
            }
            if (jRadioButton3.isSelected() == true) {
                coll = db.getCollection("test_tab3");//from database object, get collection with date
            }
            BasicDBObject query = new BasicDBObject();
            query.put("naics", jTextField4.getText().toString());
            long timeNow = System.nanoTime();
            cursor = coll.find();
            long timeAfter = System.nanoTime();
            if (cursor.count() > 0) {
                DBObject currentRecord = cursor.next();
                System.out.println("Records fetched");
            } else {
                // JOptionPane.showMessageDialog(null,"Record not found");
                System.out.println("Record not found!");
            }
            long actual = timeAfter - timeNow;
            jTextField2.setText("" + actual);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex);
        } finally {
            cursor.close();//cleanup
            mongoClient.close();
        }      
    }
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        try {
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Connection formed successfully");
            String myUrl = "jdbc:mysql://localhost/mydb";
            String pwd = "arijit";
            String query = "";
            if (jRadioButton1.isSelected() == true) {
                query = "select * from test_tab1 where naics=" + "'"
                        + jTextField4.getText().toString() + "'"; //make this query parameterized later
            }
            if (jRadioButton2.isSelected() == true) {
                query = "select * from test_tab2 where naics=" + "'"
                        + jTextField4.getText().toString() + "'"; //make this query parameterized later
            }
            if (jRadioButton3.isSelected() == true) {
                query = "select * from test_tab3 where naics=" + "'"
                        + jTextField4.getText().toString() + "'"; //make this query parameterized later
            }
            Connection con = DriverManager
                    .getConnection(myUrl, "root", pwd);
            Statement st = con.createStatement();
            long timeNow = System.nanoTime();
            ResultSet rs = st.executeQuery(query);
            long timeAfter = System.nanoTime() - timeNow;
            st.close();
            jTextField1.setText("" + timeAfter);
        } catch (Exception e) {
            System.out.println(e);
        } 
    }
}

Related Tutorials