8 Replies - 1002 Views - Last Post: 20 January 2013 - 08:33 AM Rate Topic: -----

#1 sirnur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-January 13

iterate through the map and store the entries into mysql database

Posted 20 January 2013 - 05:28 AM

How can I iterate through the map and store the entries into mysql database. I have look for example but no luck an illustration would be wonderful. p.s the code works perfectly, the output is >> USD 1.5875 EUR 1.1919 ALL 166.2959
AMD 645.4025. I need to store the county code and their rate


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
 
public class Rate {
    /** list of string array containing IOSCurrencyCodes*/
        final String[] CURRENCY = new String[] { "USD","EUR","ALL","AMD",};
        
        @SuppressWarnings("rawtypes")
		void checkRateAllAtEnd() throws Exception {     
                List<Callable<HashMap>> tasks = new ArrayList<Callable<HashMap>>();
                for (final String ccy : CURRENCY) {
                        tasks.add(new Callable<HashMap>() {
                                public HashMap<String, Comparable> call() throws Exception {
                                        return getRates(ccy);
                                }
                        });
                }
                ExecutorService executorPool = Executors.newCachedThreadPool();
                final List<Future<HashMap>> listRates = executorPool.invokeAll(tasks, 3600, TimeUnit.SECONDS);
               
                for (Future<HashMap> rate : listRates) {
                        HashMap ccyRate = rate.get();
                        System.out.println(ccyRate.get("CCY") +" "+ ccyRate.get("RATE"));}
                }
                @SuppressWarnings("rawtypes")
        		public HashMap<String, Comparable> getRates(String ccy) throws Exception {
                        URL url = new URL("http://finance.yahoo.com/d/quotes.csv?e=.csv&f=sl1d1t1&s=GBP"
                                                        + ccy + "=X");
                        BufferedReader reader = new BufferedReader(new InputStreamReader(
                                        url.openStream()));
                        
                        String data = reader.readLine();
                        String[] dataItems = data.split(",");
                        Double rate = Double.valueOf(dataItems[1]);
                        
                        HashMap<String, Comparable> ccyRate = new HashMap<String, Comparable>();
                        ccyRate.put("CCY", ccy);
                        ccyRate.put("RATE", rate);
         
                        return ccyRate;           
                }
        public static void main(String[] args) {
        	Rate ccyRate = new Rate();
                try {
                       //ccyConverter.checkRateSequential();
                       ccyRate.checkRateAllAtEnd();
                } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
        }
}


Is This A Good Question/Topic? 0
  • +

Replies To: iterate through the map and store the entries into mysql database

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2812
  • View blog
  • Posts: 11,902
  • Joined: 20-September 08

Re: iterate through the map and store the entries into mysql database

Posted 20 January 2013 - 06:19 AM

I've seen a question remarkably similar to this recently if it's not the same one. My comments are the same

a. what are the column definitions in the db?
b. where are the time/date constraints in your code? (a rate is time-specific)
Was This Post Helpful? 0
  • +
  • -

#3 sirnur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-January 13

Re: iterate through the map and store the entries into mysql database

Posted 20 January 2013 - 06:34 AM

View Postg00se, on 20 January 2013 - 06:19 AM, said:

I've seen a question remarkably similar to this recently if it's not the same one. My comments are the same

a. what are the column definitions in the db?
b. where are the time/date constraints in your code? (a rate is time-specific)



A. the column definition >> cuuntrycode varchar(10) and rate float <<<

B.think this is the time-specific >>
  ExecutorService executorPool = Executors.newCachedThreadPool();
final List<Future<HashMap>> listRates = executorPool.invokeAll(tasks, 3600, TimeUnit.SECONDS);

Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2812
  • View blog
  • Posts: 11,902
  • Joined: 20-September 08

Re: iterate through the map and store the entries into mysql database

Posted 20 January 2013 - 06:42 AM

Quote

B.think this is the time-specific >>

Nope, what i'm getting that is there needs to be a currency, rate AND time. The rate, a few seconds later could be completely different
Was This Post Helpful? 0
  • +
  • -

#5 sirnur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-January 13

Re: iterate through the map and store the entries into mysql database

Posted 20 January 2013 - 06:56 AM

View Postg00se, on 20 January 2013 - 06:42 AM, said:

Quote

B.think this is the time-specific >>

Nope, what i'm getting that is there needs to be a currency, rate AND time. The rate, a few seconds later could be completely different


I know what you mean but I only need to store the rate on time only and if I need to update the table I will drop it and create it a new one. or create a trigger if the application restarted or any button is click ..

do you know how I can iterate the HashMap and store it into my database ? i have been looking for example on that

thanks
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2812
  • View blog
  • Posts: 11,902
  • Joined: 20-September 08

Re: iterate through the map and store the entries into mysql database

Posted 20 January 2013 - 07:01 AM

Quote

I know what you mean but I only need to store the rate on time only and if I need to update the table I will drop it and create it a new one.
OK, i understand. The rates will be updated automatically.

Quote

do you know how I can iterate the HashMap and store it into my database ? i
Assuming you mean 'ccyRate', please post the output of
System.out.println(ccyRate);

Was This Post Helpful? 0
  • +
  • -

#7 sirnur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-January 13

Re: iterate through the map and store the entries into mysql database

Posted 20 January 2013 - 07:07 AM

View Postg00se, on 20 January 2013 - 07:01 AM, said:

Quote

I know what you mean but I only need to store the rate on time only and if I need to update the table I will drop it and create it a new one.
OK, i understand. The rates will be updated automatically.

Quote

do you know how I can iterate the HashMap and store it into my database ? i
Assuming you mean 'ccyRate', please post the output of
System.out.println(ccyRate);



output is
 {CCY=USD, RATE=1.5875}
USD 1.5875
{CCY=EUR, RATE=1.1919}
EUR 1.1919
{CCY=ALL, RATE=166.2959}
ALL 166.2959
{CCY=AMD, RATE=645.4025}
AMD 645.4025


[quote name='sirnur' date='20 January 2013 - 07:06 AM' timestamp='1358690768' post='1786847']

View Postg00se, on 20 January 2013 - 07:01 AM, said:

Quote

I know what you mean but I only need to store the rate on time only and if I need to update the table I will drop it and create it a new one.
OK, i understand. The rates will be updated automatically.

Quote

do you know how I can iterate the HashMap and store it into my database ? i
Assuming you mean 'ccyRate', please post the output of
System.out.println(ccyRate);



output is
{CCY=USD, RATE=1.5875}
{CCY=EUR, RATE=1.1919}
{CCY=ALL, RATE=166.2959}
{CCY=AMD, RATE=645.4025}

Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2812
  • View blog
  • Posts: 11,902
  • Joined: 20-September 08

Re: iterate through the map and store the entries into mysql database

Posted 20 January 2013 - 07:55 AM

OK, you should use a PreparedStatement using the sql String

"insert into foo (countrycode, rate) values(?, ?)"
http://docs.oracle.c...s/prepared.html

So first of all, get the framework for the insert setup using the following, and make sure it works using dummy values, then i'll show you the Map bit

public void insertRates(Map<String, Comparable> ratesMap) {
  // Insert code here
}

Was This Post Helpful? 0
  • +
  • -

#9 sirnur  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 09-January 13

Re: iterate through the map and store the entries into mysql database

Posted 20 January 2013 - 08:33 AM

View Postg00se, on 20 January 2013 - 07:55 AM, said:

OK, you should use a PreparedStatement using the sql String

"insert into foo (countrycode, rate) values(?, ?)"
http://docs.oracle.c...s/prepared.html

So first of all, get the framework for the insert setup using the following, and make sure it works using dummy values, then i'll show you the Map bit

public void insertRates(Map<String, Comparable> ratesMap) {
  // Insert code here
}


I will attempt to do that thank you
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1