I have Mysql table and I want to push returned values from query into java hashmap. I am very beginner in java. I took some help from available answers and wrote a code. Summary: I have list of country codes as connectedCountryList =[40,203,250,528] which should be the Key of the hashmap and I want to iterate over these country codes and store return values from sql table which has milions of rows. Mysql table sample data looks like-
utc date area_in area_out value
2015-12-05T03:00Z 05.12.2015 03:00 276 40 3059
2015-12-04T03:00Z 04.12.2015 03:00 276 40 2523
2015-12-07T03:00Z 07.12.2015 03:00 276 40 2873
2015-12-06T03:00Z 06.12.2015 03:00 276 40 3386
2015-12-01T03:00Z 01.12.2015 03:00 276 40 2936
2015-12-03T03:00Z 03.12.2015 03:00 276 40 2564
2016-04-01T19:00Z 01.04.2016 19:00 276 203 -291
2016-05-02T05:00Z 02.05.2016 05:00 276 203 -276
2016-05-03T05:00Z 03.05.2016 05:00 276 203 -123
2016-04-03T19:00Z 03.04.2016 19:00 276 203 -899
2016-04-02T19:00Z 02.04.2016 19:00 276 203 -1100
2016-05-01T05:00Z 01.05.2016 05:00 276 203 -45
2015-01-16T13:00Z 16.01.2015 13:00 276 203 -339
2016-04-05T19:00Z 05.04.2016 19:00 276 203 734
2015-01-17T13:00Z 17.01.2015 13:00 276 203 -300
2017-04-27T19:00Z 27.04.2017 19:00 276 250 -75
2016-09-25T23:00Z 25.09.2016 23:00 276 250 -174
2015-03-18T12:00Z 18.03.2015 12:00 276 250 338
2016-10-11T09:00Z 11.10.2016 09:00 276 250 1093
2015-03-17T12:00Z 17.03.2015 12:00 276 250 190
2017-05-24T05:00Z 24.05.2017 05:00 276 250 -454
2015-03-14T03:00Z 14.03.2015 03:00 276 528 3336
2016-05-09T00:00Z 09.05.2016 00:00 276 528 2705
2016-11-12T19:00Z 12.11.2016 19:00 276 528 3030
2015-03-15T03:00Z 15.03.2015 03:00 276 528 3182
2016-10-15T05:00Z 15.10.2016 05:00 276 528 2111
Actually I have big code and this is a small part I am working on it.
connectedCountryList =[40,203,250,528];
String tablename= "mytable";
MarketAreacode = "276"
public class Exchangeflows{
private String utc;
private int value;
public Exchangeflows(){}
public Exchangeflows(String utc,int value ){
this.utc =utc;
this.value=value;
}
public String getutc(){
return utc;
}
public void setutc(String UTC) {
this.utc = UTC;
}
public int getvalue(){
return value;
}
public void setvalue(int value) {
this.value = value;
}
}
final Map<Integer, Exchangeflows> CbExchangePower = new HashMap<Integer, Exchangeflows>();
for (final Integer countryCode : connectedCountryList) {
final String sqlquery = "select * from " + tableName +" where ((area_in = '" + marketAreaCode +"' "+" and area_out in ('"+countryCode+"'))"+
"or (area_out = '"+marketAreaCode+"' "+"and area_in in ('" + countryCode +"'))) order by utc ASC ";
conn.setResultSet(sqlquery);
// SQL query Executes perfect
while (conn.resultSet.next()){
String utc = conn.resultSet.getString("utc");
int value = conn.resultSet.getInt("value");
PowerExchange = new Exchangeflows (utc, value);
CbExchangePower.put(countryCode,PowerExchange);
}
When I run my code it reads data correclty in each iteration but every time it overwrites the values and I am getting latest values according to UTC ASC as per sql query. e.g. [2015-12-07T03:00Z 07.12.2015 03:00 276 40 2873 ]
. I do not understand how I should iterate my sql in a loop to save all content of table into hashmapin the form of <countryCode <utc, value>>
. The hashmap sould iterate through list of countries, hence country code will be unique in each iteration as per for loop and have to add each row from sql table in while loop.
Can any one suggest me any changes in my code?