I am trying to create a league table from key value pairs in a derby database - the rows in the database have only two columns, TeamName & Goals.
I need to get these to my GUI class so I can set the keys & values as JLabels in the league table. Ordered top down descending in terms of total goals.
From what I have read LinkedHashMap & TreeSet should both be able to assist me.
Code I have so far:
public TreeMap viewTeams(){
TreeMap teamData = new TreeMap();
String viewTeams = "SELECT * FROM HUI.TEAM";
connectToDatabase(dbName);
try {
stmt = dbConnection.createStatement();
rs = stmt.executeQuery(viewTeams);
} catch (SQLException error) {
System.err.println("Error querying database for teams: " + error.toString());
}
try {
while (rs.next()){
teamData.put((rs.getString("TEAMNAME")), (rs.getInt("GOALSSCORED")));
}
} catch (SQLException error) {
System.err.println("Error adding players to HasMap: " + error.toString());
}
return teamData;
}
In the TeamDB class
public void updateLeagueTable(){
TeamDB tdb = new TeamDB("FootManDatabase");
TreeMap teamData = tdb.viewTeams(); // Do I need this new TreeMap?
// How do I Iterate through the pairs in descending order?
}
In the GUI class