我有这段代码,我在数据库(mysql)中获取一些数据,并将其存储在多维hashmap中。但是"节目" part在每次迭代中都是相同的,尽管我每次迭代都会创建一个新的hashmap。
它是" idProgram"我认为导致问题的hashmap; 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
}
}
}
]
所以第一部分正确输出了更改的数据..但是&#34;程序&#34;中的子图。每次迭代都会覆盖数据:(
答案 0 :(得分:0)
是的,你绝对不想在每次迭代中声明一个新的HashMap。在外面声明并相应地插入您想要的数据
public static String epg() {
Connection con = null;
PreparedStatement pst = null;
ResultSet rs = null;
PreparedStatement pst2 = null;
ResultSet rs2 = null;
Date dNow = new Date();
LinkedHashMap<String, Object> channels = new LinkedHashMap<String, Object>();
LinkedHashMap<Integer,Object> idProgram = new LinkedHashMap<Integer,Object>();
...
...
while (rs.next()) {
channels.put("idChannel", rs.getString(1));
channels.put("channelName", rs.getString(2));
channels.put("logoPath", rs.getString(3));
channels.put("macro", rs.getString(4))
答案 1 :(得分:0)
您对pst2的查询总是相同的,当然每次都会提供相同的程序数据。您是否打算为每个频道更新“时间”?如果没有,该程序如何与您的频道相关,它们的共同密钥是什么?然后,应在pst2 SQL查询中使用此密钥来查询所需的密钥。