Hashmaps getting overwritten, though new maps are created each iteration
<p>I have this piece of code, where i'm fetching some data in a database (mysql), and storing it in a multidimensional hashmap. But the "programs" part is the same in every iteration, though i AM creating a new hashmap every iteration..</p>

<p>It's the "idProgram" hashmap causing the problem, i think ;s</p>

<p>Here is the code:</p>

<pre><code>public static String epg() {
    Connection con = null;
    PreparedStatement pst = null;
    ResultSet rs = null;
    PreparedStatement pst2 = null;
    ResultSet rs2 = null;
    Date dNow = new Date();

    SimpleDateFormat ft = new SimpleDateFormat ("yyyyMMdd");
    String time = ft.format(dNow).toString();
    String json = "[";
    String url = "jdbc:mysql://" + main_server.ipSQL + ":" + main_server.portSQL + "/" + main_server.dbExtra;
    try {
        con = DriverManager.getConnection(url, main_server.userSQL, main_server.passwordSQL);
        pst = con.prepareStatement("SELECT idChannel, channelName, logoPath, macro FROM channel");
        rs = pst.executeQuery();
        Gson gson = new GsonBuilder().create();
        while (rs.next()) { 
            LinkedHashMap&lt;String, Object&gt; channels = new LinkedHashMap&lt;String, Object&gt;();
            LinkedHashMap&lt;Integer,Object&gt; idProgram = new LinkedHashMap&lt;Integer,Object&gt;();
            channels.put("idChannel", rs.getString(1));
            channels.put("channelName", rs.getString(2));
            channels.put("logoPath", rs.getString(3));
            channels.put("macro", rs.getString(4));
            pst2 = con.prepareStatement("SELECT idProgram, title, start, stop, category, episodeNumber FROM program WHERE start LIKE '"+time+"%'");
            rs2 = pst2.executeQuery();
            while (rs2.next()) { 
                LinkedHashMap&lt;String, Object&gt; channels2 = new LinkedHashMap&lt;String, Object&gt;();
                int duration = Integer.parseInt(rs2.getString(4).substring(8, 12)) - Integer.parseInt(rs2.getString(3).substring(8, 12));
                channels2.put("title", StringEscapeUtils.unescapeJava(rs2.getString(2)));
                channels2.put("category", StringEscapeUtils.unescapeJava(rs2.getString(5)));
                channels2.put("episodeNumber", rs2.getString(6));
                channels2.put("start", rs2.getLong(4));
                channels2.put("duration", duration);
                idProgram.put(rs2.getInt(1), channels2);
            }  
            channels.put("programs", idProgram);
            json = json + gson.toJson(channels) + ",";
        }  
        json = json.substring(0, json.length() - 1) + "]";
    } catch (SQLException ex) {
        Logger lgr = Logger.getLogger(sql.class.getName());
        lgr.log(Level.SEVERE, ex.getMessage(), ex);
    } finally {
        try {
            if (rs != null) {
                rs.close();
            }
            if (pst != null) {
                pst.close();
            }
            if (con != null) {
                con.close();
            }
        } catch (SQLException ex) {
            Logger lgr = Logger.getLogger(sql.class.getName());
            lgr.log(Level.WARNING, ex.getMessage(), ex);
        }
    }
    try {
        BufferedWriter out = new BufferedWriter(new FileWriter("file.txt"));
        out.write(json);
        out.close();
    } catch (IOException e) {}
    return json;
}
</code></pre>

<p><strong>EDIT:</strong></p>

<p>This code outputs:</p>

<pre><code>[
{
"idChannel": "1",
"channelName": "DR1 HD",
"logoPath": "/media/channel_logo/1.jpg",
"macro": "TV,1,ENTER",
"programs": {
  "55483": {
    "title": "Drivhusdrømme",
    "category": "documentary, serie",
    "episodeNumber": " .2/6. ",
    "start": 20130505065500,
    "duration": 30
  },
  "55484": {
    "title": "Der er noget i luften",
    "category": "serie",
    "episodeNumber": " .10/320. ",
    "start": 20130505072000,
    "duration": 65
  },
  "55485": {
    "title": "Der er noget i luften",
    "category": "serie",
    "episodeNumber": " .11/320. ",
    "start": 20130505075000,
    "duration": 30
  },
  "55486": {
    "title": "Der er noget i luften",
    "category": "serie",
    "episodeNumber": " .12/320. ",
    "start": 20130505081500,
    "duration": 65
  },
  "55487": {
    "title": "Der er noget i luften",
    "category": "serie",
    "episodeNumber": " .13/320. ",
    "start": 20130505084000,
    "duration": 25
  },
  "55488": {
    "title": "Der er noget i luften",
    "category": "serie",
    "episodeNumber": " .14/320. ",
    "start": 20130505091000,
    "duration": 70
  },
  "55489": {
    "title": "Downton Abbey III",
    "category": "serie",
    "episodeNumber": " .7/10. ",
    "start": 20130505101500,
    "duration": 105
  },
  "55490": {
    "title": "Downton Abbey III",
    "category": "serie",
    "episodeNumber": " .8/10. ",
    "start": 20130505110500,
    "duration": 90
  },
  "55491": {
    "title": "Kyst til kyst - Østjyllands kyst",
    "category": "entertainment, serie",
    "episodeNumber": " .2/8. ",
    "start": 20130505120500,
    "duration": 100
  },
  "55492": {
    "title": "OBS",
    "category": "news, serie",
    "start": 20130505121000,
    "duration": 5
  },
  "55493": {
    "title": "BingoBoxen",
    "category": "entertainment, serie",
    "start": 20130505122500,
    "duration": 15
  },
  "55494": {
    "title": "General Custers sidste kamp",
    "category": "movie",
    "start": 20130505144000,
    "duration": 215
  },
  "55495": {
    "title": "For godt til at være sandt",
    "category": "entertainment, serie",
    "start": 20130505150000,
    "duration": 60
  },
  "55496": {
    "title": "Miss Marple: Den gustne hest",
    "category": "movie",
    "start": 20130505163000,
    "duration": 130
  },
  "55497": {
    "title": "HåndboldSøndag: Team Tvis Holstebro-Metz Handball (k), direkte",
    "category": "sport",
    "start": 20130505183000,
    "duration": 200
  },
  "55498": {
    "title": "TV AVISEN  med Sporten og Vejret",
    "category": "news, serie",
    "start": 20130505190000,
    "duration": 70
  },
  "55499": {
    "title": "Menneskets planet",
    "category": "documentary, serie",
    "episodeNumber": " .7/8. ",
    "start": 20130505194500,
    "duration": 45
  },
  "55500": {
    "title": "Bag om Menneskets planet",
    "category": "documentary, serie",
    "episodeNumber": " .7/8. ",
    "start": 20130505200000,
    "duration": 55
  }
}
},{
"idChannel": "2",
"channelName": "TV2 HD",
"logoPath": "/media/channel_logo/2.jpg",
"macro": "TV,2,ENTER",
"programs": {
  "55483": {
    "title": "Drivhusdrømme",
    "category": "documentary, serie",
    "episodeNumber": " .2/6. ",
    "start": 20130505065500,
    "duration": 30
  },
  "55484": {
    "title": "Der er noget i luften",
    "category": "serie",
    "episodeNumber": " .10/320. ",
    "start": 20130505072000,
    "duration": 65
  },
  "55485": {
    "title": "Der er noget i luften",
    "category": "serie",
    "episodeNumber": " .11/320. ",
    "start": 20130505075000,
    "duration": 30
  },
  "55486": {
    "title": "Der er noget i luften",
    "category": "serie",
    "episodeNumber": " .12/320. ",
    "start": 20130505081500,
    "duration": 65
  },
  "55487": {
    "title": "Der er noget i luften",
    "category": "serie",
    "episodeNumber": " .13/320. ",
    "start": 20130505084000,
    "duration": 25
  },
  "55488": {
    "title": "Der er noget i luften",
    "category": "serie",
    "episodeNumber": " .14/320. ",
    "start": 20130505091000,
    "duration": 70
  },
  "55489": {
    "title": "Downton Abbey III",
    "category": "serie",
    "episodeNumber": " .7/10. ",
    "start": 20130505101500,
    "duration": 105
  },
  "55490": {
    "title": "Downton Abbey III",
    "category": "serie",
    "episodeNumber": " .8/10. ",
    "start": 20130505110500,
    "duration": 90
  },
  "55491": {
    "title": "Kyst til kyst - Østjyllands kyst",
    "category": "entertainment, serie",
    "episodeNumber": " .2/8. ",
    "start": 20130505120500,
    "duration": 100
  },
  "55492": {
    "title": "OBS",
    "category": "news, serie",
    "start": 20130505121000,
    "duration": 5
  },
  "55493": {
    "title": "BingoBoxen",
    "category": "entertainment, serie",
    "start": 20130505122500,
    "duration": 15
  },
  "55494": {
    "title": "General Custers sidste kamp",
    "category": "movie",
    "start": 20130505144000,
    "duration": 215
  },
  "55495": {
    "title": "For godt til at være sandt",
    "category": "entertainment, serie",
    "start": 20130505150000,
    "duration": 60
  },
  "55496": {
    "title": "Miss Marple: Den gustne hest",
    "category": "movie",
    "start": 20130505163000,
    "duration": 130
  },
  "55497": {
    "title": "HåndboldSøndag: Team Tvis Holstebro-Metz Handball (k), direkte",
    "category": "sport",
    "start": 20130505183000,
    "duration": 200
  },
  "55498": {
    "title": "TV AVISEN  med Sporten og Vejret",
    "category": "news, serie",
    "start": 20130505190000,
    "duration": 70
  },
  "55499": {
    "title": "Menneskets planet",
    "category": "documentary, serie",
    "episodeNumber": " .7/8. ",
    "start": 20130505194500,
    "duration": 45
  },
  "55500": {
    "title": "Bag om Menneskets planet",
    "category": "documentary, serie",
    "episodeNumber": " .7/8. ",
    "start": 20130505200000,
    "duration": 55
  }
}
}
]
</code></pre>

<p>So the first part is correctly output with changed data.. but the submap in "programs" overwrite the data every iteration :(</p>
