我有这段代码,我在数据库(mysql)中获取一些数据,并将其存储在多维哈希图中。但是“程序”部分在每次迭代中都是相同的,尽管我在每次迭代中都创建了一个新的哈希图。
我认为这是导致问题的“idProgram”哈希图;s
这是代码:
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<String, Object> channels = new LinkedHashMap<String, Object>();
LinkedHashMap<Integer,Object> idProgram = new LinkedHashMap<Integer,Object>();
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<String, Object> channels2 = new LinkedHashMap<String, Object>();
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;
}
编辑:
此代码输出:
[
{
"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
}
}
}
]
所以第一部分正确输出了更改的数据..但是“程序”中的子图每次迭代都会覆盖数据:(