Insert into mongodb and mysql - Java JDBC

Java examples for JDBC:MySQL

Description

Insert into mongodb and mysql

Demo Code


import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
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.*;
import java.io.*;
import java.util.*;


public class Insertion extends javax.swing.JFrame {


    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        BufferedReader br = null;
        try {/* w w w  . java  2s.co m*/
            br = new BufferedReader(new FileReader(
                    "C:\\xampp\\mysql\\data\\mydb\\join1.txt"));
            if (br == null) {
                System.out.println("File note found");
            } else {
                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);
                int count = 0;
                long total_time = 0;
                while ((count++) < 100) {
                    String[] results = br.readLine().split(",\\s*");
                    String insertQuery = "INSERT INTO insert_tab values(?,?,?)";
                    PreparedStatement ps = null;
                    ps = con.prepareStatement(insertQuery);
                    ps.setString(1, results[0]);
                    ps.setString(2, results[1]);
                    ps.setString(3, results[2]);
                    long timeBefore = System.nanoTime();
                    ps.execute();
                    long timeAfter = System.nanoTime() - timeBefore;
                    total_time += timeAfter;
                }
                jTextField1.setText("" + total_time);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        MongoClient mongo = null;
        DBCursor cursor = null;
        DBCollection coll = null;
        DB db = null;
        BufferedReader br = null;
        try {
            mongo = new MongoClient("localhost", 27017);
            br = new BufferedReader(new FileReader(
                    "C:\\xampp\\mysql\\data\\mydb\\join1.txt"));
            if (br == null) {
                System.out.println("File note found");
            }
        } 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;
        }
        try {
            int count = 0;
            long total_time = 0;
            while ((count++) < 100) {
                String[] results = br.readLine().split(",\\s*");

                BasicDBObject query = new BasicDBObject();
                query.put("field1", results[0]);
                query.put("field2", results[1]);
                query.put("field3", results[2]);
                long timeBefore = System.nanoTime();
                coll.insert(query);
                long timeAfter = System.nanoTime() - timeBefore;
                total_time += timeAfter;
            }
            jTextField1.setText("" + total_time);
            // TODO add your handling code here:
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }

}

Related Tutorials