com.mycompany.mavenproject4.NewMain.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.mavenproject4.NewMain.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.mavenproject4;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.*;
import org.apache.commons.dbcp.*;
import com.csvreader.CsvReader;
import com.csvreader.CsvWriter;
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.DBCursor;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.FindIterable;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import redis.clients.jedis.Jedis;

/**
 *
 * @author SuperUser
 */
public class NewMain {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        System.out.println("Enter your choice do you want to work with 1.postgre 2.Redis 3.Mongodb");
        InputStreamReader IORdatabase = new InputStreamReader(System.in);
        BufferedReader BIOdatabase = new BufferedReader(IORdatabase);
        String databasechoice = null;
        try {
            databasechoice = BIOdatabase.readLine();
        } catch (Exception E7) {
            System.out.println(E7.getMessage());
        }

        // loading data from the CSV file 

        CsvReader Employees = null;

        try {
            Employees = new CsvReader("CSVFile\\Employees.csv");
        } catch (FileNotFoundException EB) {
            System.out.println(EB.getMessage());
        }
        int ColumnCount = 3;
        int RowCount = 100;
        int iloop = 0;

        try {
            Employees = new CsvReader("CSVFile\\Employees.csv");
        } catch (FileNotFoundException E) {
            System.out.println(E.getMessage());
        }

        String[][] Dataarray = new String[RowCount][ColumnCount];
        try {
            while (Employees.readRecord()) {
                String v;
                String[] x;
                v = Employees.getRawRecord();
                x = v.split(",");
                for (int j = 0; j < ColumnCount; j++) {
                    String value = null;
                    int z = j;
                    value = x[z];
                    try {
                        Dataarray[iloop][j] = value;
                    } catch (Exception E) {
                        System.out.println(E.getMessage());
                    }
                    // System.out.println(Dataarray[iloop][j]);
                }
                iloop = iloop + 1;
            }
        } catch (IOException Em) {
            System.out.println(Em.getMessage());
        }

        Employees.close();

        // connection to Database 
        switch (databasechoice) {
        // postgre code goes here 
        case "1":

            Connection Conn = null;
            Statement Stmt = null;
            URI dbUri = null;
            String choice = null;
            InputStreamReader objin = new InputStreamReader(System.in);
            BufferedReader objbuf = new BufferedReader(objin);
            try {
                Class.forName("org.postgresql.Driver");
            } catch (Exception E1) {
                System.out.println(E1.getMessage());
            }
            String username = "ldoiarosfrzrua";
            String password = "HBkE_pJpK5mMIg3p2q1_odmEFX";
            String dbUrl = "jdbc:postgresql://ec2-54-83-53-120.compute-1.amazonaws.com:5432/d6693oljh5cco4?ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory";

            // now update data in the postgress Database 

            // for(int i=0;i<RowCount;i++)
            // {
            //       try 
            //          {

            //         Conn= DriverManager.getConnection(dbUrl, username, password);    
            //           Stmt = Conn.createStatement();
            //           String Connection_String = "insert into EmployeeDistinct (name,jobtitle,department) values (' "+ Dataarray[i][0]+" ',' "+ Dataarray[i][1]+" ',' "+ Dataarray[i][2]+" ')";
            //         Stmt.executeUpdate(Connection_String);
            //       
            //        }
            //        catch(SQLException E4)
            //         {
            //             System.out.println(E4.getMessage());
            //          }
            // }

            // Quering with the Database 

            System.out.println("1. Display Data ");
            System.out.println("2. Select data based on primary key");
            System.out.println("3. Select data based on Other attributes e.g Name");
            System.out.println("Enter your Choice ");
            try {
                choice = objbuf.readLine();
            } catch (IOException E) {
                System.out.println(E.getMessage());
            }
            switch (choice) {
            case "1":
                try {
                    Conn = DriverManager.getConnection(dbUrl, username, password);
                    Stmt = Conn.createStatement();
                    String Connection_String = " Select * from EmployeeDistinct;";
                    ResultSet objRS = Stmt.executeQuery(Connection_String);
                    while (objRS.next()) {
                        System.out.println("Employee ID: " + objRS.getInt("EmployeeID"));
                        System.out.println("Employee Name: " + objRS.getString("Name"));
                        System.out.println("Employee City: " + objRS.getString("Jobtitle"));
                        System.out.println("Employee City: " + objRS.getString("Department"));
                    }

                } catch (Exception E2) {
                    System.out.println(E2.getMessage());
                }

                break;

            case "2":

                System.out.println("Enter the Primary Key");
                InputStreamReader objkey = new InputStreamReader(System.in);
                BufferedReader objbufkey = new BufferedReader(objkey);
                int key = 0;
                try {
                    key = Integer.parseInt(objbufkey.readLine());
                } catch (IOException E) {
                    System.out.println(E.getMessage());
                }
                try {
                    Conn = DriverManager.getConnection(dbUrl, username, password);
                    Stmt = Conn.createStatement();
                    String Connection_String = " Select * from EmployeeDistinct where EmployeeID=" + key + ";";
                    ResultSet objRS = Stmt.executeQuery(Connection_String);
                    while (objRS.next()) {
                        System.out.println("Employee Name: " + objRS.getString("Name"));
                        System.out.println("Employee City: " + objRS.getString("Jobtitle"));
                        System.out.println("Employee City: " + objRS.getString("Department"));
                    }

                } catch (Exception E2) {
                    System.out.println(E2.getMessage());
                }
                break;

            case "3":
                String Name = null;
                System.out.println("Enter Name to find the record");
                InputStreamReader objname = new InputStreamReader(System.in);
                BufferedReader objbufname = new BufferedReader(objname);

                try {
                    Name = (objbufname.readLine()).toString();
                } catch (IOException E) {
                    System.out.println(E.getMessage());
                }
                try {
                    Conn = DriverManager.getConnection(dbUrl, username, password);
                    Stmt = Conn.createStatement();
                    String Connection_String = " Select * from EmployeeDistinct where Name=" + "'" + Name + "'"
                            + ";";

                    ResultSet objRS = Stmt.executeQuery(Connection_String);
                    while (objRS.next()) {
                        System.out.println("Employee ID: " + objRS.getInt("EmployeeID"));
                        System.out.println("Employee City: " + objRS.getString("Jobtitle"));
                        System.out.println("Employee City: " + objRS.getString("Department"));
                    }

                } catch (Exception E2) {
                    System.out.println(E2.getMessage());
                }
                break;
            }
            try {
                Conn.close();
            } catch (SQLException E6) {
                System.out.println(E6.getMessage());
            }
            break;

        // Redis code goes here 

        case "2":
            int Length = 0;
            String ID = null;
            Length = 100;

            // Connection to Redis 
            Jedis jedis = new Jedis("pub-redis-18017.us-east-1-4.6.ec2.redislabs.com", 18017);
            jedis.auth("7EFOisRwMu8zvhin");
            System.out.println("Connected to Redis");

            // Storing values in the database 

            //  System.out.println("Storing values in the Database might take a minute ");

            // for(int i=0;i<Length;i++)
            // { 
            // Store data in redis 
            //     int j=i+1;
            //  jedis.hset("Employe:" + j , "Name", Dataarray[i][0]);
            //   jedis.hset("Employe:" + j , "Jobtitle ", Dataarray[i][1]);
            //  jedis.hset("Employe:" + j , "Department", Dataarray[i][2]);

            //  }

            System.out.println("Search by 1.primary key,2.Name 3.display first 20");
            InputStreamReader objkey = new InputStreamReader(System.in);
            BufferedReader objbufkey = new BufferedReader(objkey);
            String interimChoice1 = null;
            try {
                interimChoice1 = objbufkey.readLine();
            } catch (IOException E) {
                System.out.println(E.getMessage());
            }
            switch (interimChoice1) {
            case "1":
                System.out.print("Get details using the primary Key");
                InputStreamReader IORKey = new InputStreamReader(System.in);
                BufferedReader BIORKey = new BufferedReader(IORKey);
                String ID1 = null;
                try {
                    ID1 = BIORKey.readLine();
                } catch (IOException E3) {
                    System.out.println(E3.getMessage());
                }

                Map<String, String> properties = jedis.hgetAll("Employe:" + Integer.parseInt(ID1));
                for (Map.Entry<String, String> entry : properties.entrySet()) {
                    System.out.println("Name:" + jedis.hget("Employee:" + Integer.parseInt(ID1), "Name"));
                    System.out.println("Jobtitle:" + jedis.hget("Employee:" + Integer.parseInt(ID1), "Jobtitle"));
                    System.out
                            .println("Department:" + jedis.hget("Employee:" + Integer.parseInt(ID1), "Department"));
                }
                break;

            case "2":
                System.out.print("Get details using Department");
                InputStreamReader IORName1 = new InputStreamReader(System.in);
                BufferedReader BIORName1 = new BufferedReader(IORName1);

                String ID2 = null;
                try {
                    ID2 = BIORName1.readLine();
                } catch (IOException E3) {
                    System.out.println(E3.getMessage());
                }
                for (int i = 0; i < 100; i++) {
                    Map<String, String> properties3 = jedis.hgetAll("Employe:" + i);
                    for (Map.Entry<String, String> entry : properties3.entrySet()) {
                        String value = entry.getValue();
                        if (entry.getValue().equals(ID2)) {
                            System.out.println("Name:" + jedis.hget("Employee:" + i, "Name"));
                            System.out.println("Jobtitle:" + jedis.hget("Employee:" + i, "Jobtitle"));
                            System.out.println("Department:" + jedis.hget("Employee:" + i, "Department"));
                        }

                    }
                }

                //for (Map.Entry<String, String> entry : properties1.entrySet())
                //{
                //System.out.println(entry.getKey() + "---" + entry.getValue());
                // }
                break;

            case "3":
                for (int i = 1; i < 21; i++) {
                    Map<String, String> properties3 = jedis.hgetAll("Employe:" + i);
                    for (Map.Entry<String, String> entry : properties3.entrySet()) {

                        // System.out.println(jedis.hget("Employee:" + i,"Name"));
                        System.out.println("Name:" + jedis.hget("Employee:" + i, "Name"));
                        System.out.println("Jobtitle:" + jedis.hget("Employee:" + i, "Jobtitle"));
                        System.out.println("Department:" + jedis.hget("Employee:" + i, "Department"));

                    }
                }
                break;
            }

            break;

        // mongo db code goes here 

        case "3":
            MongoClient mongo = new MongoClient(
                    new MongoClientURI("mongodb://root2:12345@ds055564.mongolab.com:55564/heroku_766dnj8c"));
            DB db;
            db = mongo.getDB("heroku_766dnj8c");
            // storing values in the database 
            //  for(int i=0;i<100;i++)
            // {
            //     BasicDBObject document = new BasicDBObject();
            //    document.put("_id", i+1);
            //    document.put("Name", Dataarray[i][0]);
            //    document.put("Jobtitle", Dataarray[i][1]);    
            //    document.put("Department", Dataarray[i][2]);    
            //    db.getCollection("EmployeeRaw").insert(document);

            // }
            System.out.println("Search by 1.primary key,2.Name 3.display first 20");
            InputStreamReader objkey6 = new InputStreamReader(System.in);
            BufferedReader objbufkey6 = new BufferedReader(objkey6);
            String interimChoice = null;
            try {
                interimChoice = objbufkey6.readLine();
            } catch (IOException E) {
                System.out.println(E.getMessage());
            }
            switch (interimChoice) {
            case "1":
                System.out.println("Enter the Primary Key");
                InputStreamReader IORPkey = new InputStreamReader(System.in);
                BufferedReader BIORPkey = new BufferedReader(IORPkey);
                int Pkey = 0;
                try {
                    Pkey = Integer.parseInt(BIORPkey.readLine());
                } catch (IOException E) {
                    System.out.println(E.getMessage());
                }
                BasicDBObject inQuery = new BasicDBObject();
                inQuery.put("_id", Pkey);
                DBCursor cursor = db.getCollection("EmployeeRaw").find(inQuery);
                while (cursor.hasNext()) {
                    // System.out.println(cursor.next());
                    System.out.println(cursor.next());
                }
                break;
            case "2":
                System.out.println("Enter the Name to Search");
                InputStreamReader objName = new InputStreamReader(System.in);
                BufferedReader objbufName = new BufferedReader(objName);
                String Name = null;
                try {
                    Name = objbufName.readLine();
                } catch (IOException E) {
                    System.out.println(E.getMessage());
                }
                BasicDBObject inQuery1 = new BasicDBObject();
                inQuery1.put("Name", Name);
                DBCursor cursor1 = db.getCollection("EmployeeRaw").find(inQuery1);
                while (cursor1.hasNext()) {
                    // System.out.println(cursor.next());
                    System.out.println(cursor1.next());
                }
                break;

            case "3":
                for (int i = 1; i < 21; i++) {
                    BasicDBObject inQuerya = new BasicDBObject();
                    inQuerya.put("_id", i);
                    DBCursor cursora = db.getCollection("EmployeeRaw").find(inQuerya);
                    while (cursora.hasNext()) {
                        // System.out.println(cursor.next());
                        System.out.println(cursora.next());
                    }
                }
                break;
            }
            break;

        }

    }

}