com.mycompany.couchdb.CsvManage.java Source code

Java tutorial

Introduction

Here is the source code for com.mycompany.couchdb.CsvManage.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.couchdb;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

/**
 *
 * @author Omer Javaid
 */

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;

import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.lightcouch.CouchDbClient;
import org.lightcouch.Response;
import com.mycompany.couchdb.DataDealer;

/**
 *
 * @author MaryamBaig
 */
public class CsvManage {

    public boolean hello() throws FileNotFoundException, IOException {
        int count = 0;
        String filename = "src/main/resources/GeoLiteCity-Location.csv";
        City city;
        DataDealer cityDao;

        cityDao = new DataDealer();

        Reader in = new FileReader(filename); //read file
        CSVParser parser = new CSVParser(in, CSVFormat.EXCEL.withHeader()); //create parser
        city = new City();
        for (CSVRecord record : parser) {
            //parse file and create objects

            //city.setLocId(Integer.parseInt(record.get("locId")));
            //city.setCountry(record.get("country"));
            city.setCity(record.get("city"));
            //city.setRegion(record.get("region"));
            city.setLatitude(Float.parseFloat(record.get("latitude")));
            city.setLongitude(Float.parseFloat(record.get("longitude")));

            //

            try {
                cityDao.save(city);

            } finally {
                cityDao.finalize();
            }

        }
        return true;
    }

    /* public void save_data(String [] columns){
    City city;
        
    DataDealer pusher;
            
    city=new City();
     city.setLocId(Integer.parseInt(columns[0]));
    city.setCountry(columns[1]);
    city.setCity(columns[3]);
    city.setRegion(columns[2]);
    city.setLatitude(Float.parseFloat(columns[5]));
    city.setLongitude(Float.parseFloat(columns[6]));
    pusher = new DataDealer();
    pusher.save(city);
        
     }
     public void read() throws FileNotFoundException, IOException {
        
        
    String path="C:\\Users\\Omer Javaid\\IdeaProjects\\lab5\\GeoLiteCity-Location.csv";
    BufferedReader br = null;
    String line = "";
        
    try {
        
        
        br = new BufferedReader(new FileReader(path));
        for(int i=0; i<2;i++){
            line=br.readLine();
        }
        
        while ((line = br.readLine()) != null ) {
        
            String[] columns = line.split(",");
            save_data(columns);
        
        
        }
        
        
        
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
        
    //return true;
        
     }
    */

}