I was wondering how I could set multiple parameters to different values, so I can get 4 different results from the same query. Can you do a loop? If yes, then how would you do it, and can you use the same executeQuery?
mapinfo = c.prepareStatement(
" SELECT TOP 1 M.ID, M.Name, COUNT(DISTINCT C.Name) 'Cities' , "
+ " COUNT(DISTINCT R2.IDfrom + R2.IDto) Roads, (SELECT AVG(R.Distance) 'Average' FROM ROAD R WHERE R.MapID = ?) Average, "
+ " MAX(R.Distance) 'Max Distance', C2.Name 'Start city', C1.Name 'End city' "
+ " FROM MAP M "
+ " LEFT JOIN ROAD R ON R.MapID = R.MapID "
+ " LEFT JOIN CITY C ON C.MapID = R.MapID "
+ " JOIN CITY C1 ON R.IDfrom = C1.ID "
+ " JOIN CITY C2 ON R.IDto = C2.ID "
+ " INNER JOIN ROAD R2 ON M.ID = R2.MapID "
+ " WHERE M.ID = ? AND C.MapID = ? AND R.MapID = ? AND R2.IDfrom < R2.IDto "
+ " GROUP BY M.ID, M.Name, R.MapID, C.MapID, C1.Name, C2.Name "
+ " ORDER BY 'Max Distance' DESC "
);
// Execute MapInfo and loop through the result
mapinfo.setInt(1, 1);
mapinfo.setInt(2, 1);
mapinfo.setInt(3, 1);
mapinfo.setInt(4, 1);
mapinfo.setInt(1, 2);
mapinfo.setInt(2, 2); //I wanna get a different result with setting the parameters to a differnt value
mapinfo.setInt(3, 2);
mapinfo.setInt(4, 2);
mapresult = mapinfo.executeQuery();
while (mapresult.next()) {
// retrieve result
int mapid = mapresult.getInt(1);
String mapname = mapresult.getString(2);
int numOfCities = mapresult.getInt(3);
int numOfRoads = mapresult.getInt(4);
int maxDistance = mapresult.getInt(5);
int average = mapresult.getInt(6);
String start = mapresult.getString(7);
String end = mapresult.getString(8);
// write output
Terminal.put("---------------------------------------\n" + "Map: " + mapname + " (" + mapid + ")\n"
+ "Cities: " + numOfCities + "\n" + "Roads: " + numOfRoads + "\n" + "Average Road Length: "
+ average + " km" + "\n" + "The longest road runs: " + maxDistance + " km" + "\n" + "Start: "
+ start + "\n" + "End: " + end + "\n" + "---------------------------------------");
}
mapresult.close();