a.A.java Source code

Java tutorial

Introduction

Here is the source code for a.A.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 a;

import java.io.BufferedReader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.hibernate.criterion.Restrictions;
import java.io.IOException;
import javax.persistence.*;
import java.util.List;
import java.util.Iterator;
import org.hibernate.HibernateException;
import java.util.*;

/**
 *
 * @author hp
 */
public class A {

    /**
     * @param args the command line arguments
     */

    /**
     *
     * @author hp
     */

    public static int count;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        String URL = "jdbc:mysql://localhost/MyBD";
        String USER = "root";
        String PASSWORD = "root";
        String DRIVER_CLASS = "com.mysql.jdbc.Driver";

        try {

            Class.forName(DRIVER_CLASS);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        Connection connection = null;
        try {

            connection = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (SQLException e) {
            System.out.println("ERROR: Unable to Connect to Database.");
        }

        Configuration cfg = new Configuration();
        cfg.configure("a/hibernate.cfg.xml");//populates the data of the configuration file  
        //creating seession factory object  
        ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(cfg.getProperties())
                .build();
        SessionFactory factory = cfg.buildSessionFactory(serviceRegistry);

        String line;
        String csv = "C:\\Users\\hp\\Desktop\\GeoLiteCity-Location.csv";
        try (BufferedReader br = new BufferedReader(new FileReader(csv))) {

            count = 0;
            City e1 = new City();

            while ((line = br.readLine()) != null) {
                if (count > 1) {
                    Session session = factory.openSession();
                    // creating transaction object  
                    Transaction t = session.beginTransaction();
                    System.out.println(line);
                    String[] s = line.split("[,]");
                    System.out.println(s[0]);
                    System.out.println(s[1]);
                    System.out.println(Float.parseFloat(s[5]));
                    System.out.println(Float.parseFloat(s[6]));
                    e1.setId(Integer.parseInt(s[0]));
                    e1.setName(s[1]);
                    e1.setC(s[3]);
                    e1.setlat(Float.parseFloat(s[5]));
                    e1.setlon(Float.parseFloat(s[6]));
                    session.save(e1);
                    t.commit();//transaction is commited  
                    session.close();
                } else {
                    count++;
                }
            }

            //                Session currentSession = factory.openSession();  
            //                List <City> list=null;
            //    //  while(list==null){
            //    list = currentSession.createCriteria(City.class)  
            //                             .add(Restrictions.eq("Name", "NL"))  
            //                             .list();
            //      for(int i=0;i<list.size();i++){
            //          
            //    System.out.println(list.get(i));
            //            }

        } catch (IOException e) {
            e.printStackTrace();
        }
        String a, b;
        float lats = 0;
        float lats2 = 0;
        float longs = 0;
        float longs2 = 0;
        System.out.print("Enter name of first city");
        Distance d1 = new Distance();

        Scanner scanner = new Scanner(System.in);
        a = scanner.nextLine();
        a = "\"" + a + "\"";
        System.out.print("Enter name of second city");
        //Scanner scanner=new Scanner(System.in);
        b = scanner.nextLine();
        b = "\"" + b + "\"";
        Transaction tx;

        Session session = factory.openSession();
        try {
            // int index=0;
            tx = session.beginTransaction();
            List employees = session.createQuery("FROM City").list();
            for (Iterator iterator = employees.iterator(); iterator.hasNext();) {
                City c = (City) iterator.next();

                if (a.contentEquals(c.getC())) {

                    System.out.print("Name: " + c.getName());
                    System.out.print(" City: " + c.getC());
                    lats = c.getlon();
                    longs = c.getlat();
                    System.out.print("  Longitude: " + c.getlon());
                    System.out.println("  Latitude: " + c.getlat());
                    break;
                }
            }
            List<String> citynames = new ArrayList<String>();
            List<Double> doubleList = new ArrayList<Double>();
            System.out.println("Enter number of cities");
            Scanner s = new Scanner(System.in);
            int num = Integer.parseInt(s.nextLine());

            Distance obj = new Distance();
            int i = 0;
            for (Iterator iterator = employees.iterator(); iterator.hasNext();) {
                City c = (City) iterator.next();
                if (i < num) {
                    doubleList.add(obj.GreatD(lats, longs, c.getlat(), c.getlon()));
                    citynames.add(c.getC());
                    i++;
                } else {
                    //get max value from list
                    Integer j = 0, maxIndex = -1;
                    Double max = null;
                    for (Double x : doubleList) {
                        if ((x != null) && ((max == null) || (x > max))) {
                            max = x;
                            maxIndex = j;
                        }
                        j++;
                    }
                    double dare = obj.GreatD(lats, longs, c.getlat(), c.getlon());
                    if (dare < max) {
                        citynames.set(maxIndex, c.getC());
                        doubleList.set(maxIndex, dare);
                        //list
                    }
                }
                // For first n values add to list

            }
            for (int k = 0; k < citynames.size(); k++) {
                System.out.println(citynames.get(k));
            }
            //         

            int check = 0;
            for (Iterator iterator = employees.iterator(); iterator.hasNext();) {
                City c = (City) iterator.next();

                if (check == 2) {
                    break;
                } else {
                    if (b.contentEquals(c.getC())) {

                        System.out.print("Name: " + c.getName());
                        System.out.print(" City: " + c.getC());
                        longs = c.getlon();
                        lats = c.getlat();
                        System.out.print("  Longitude: " + c.getlon());
                        System.out.println("  Latitude: " + c.getlat());
                        check++;
                    }
                    if (a.contentEquals(c.getC())) {

                        System.out.print("Name: " + c.getName());
                        System.out.print(" City: " + c.getC());
                        longs2 = c.getlon();
                        lats2 = c.getlat();
                        System.out.print("  Longitude: " + c.getlon());
                        System.out.println("  Latitude: " + c.getlat());
                        check++;
                    }
                }
            }
            System.out.println(d1.GreatD(lats, longs, lats2, longs2));

            tx.commit();
            return;
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}