상당히 많은 코드가 포함되어 있으며 세부 사항을 자세히 입력 할 수는 없지만 MySQL 테이블의 데이터로 트리 맵을 채우고 있습니다. 그것을 통해 반복 iterating.MySQL 테이블에서 채워진 배열을 반복합니다.
다음은 트리 맵
public class SeasonResults{
private static Map<String,SeasonResults> allResults = new TreeMap<String,SeasonResults>();
private String hometeam;
private String awayteam;
private String result;
private static SeasonResults result6;
public static SeasonResults add(String hometeam, String awayteam, String result)
{
result6 = new SeasonResults(hometeam, awayteam, result);
allResults.put(hometeam,result6);
return result6;
}
private SeasonResults(String hometeam, String awayteam, String result)
{
this.hometeam = hometeam;
this.awayteam = awayteam;
this.result = result;
}
public static Collection<SeasonResults> getCollection()
{
return allResults.values();
}
@Override
public String toString()
{
return " "+hometeam+", "+awayteam+", "+result;
}
} 여기
그리고이 포함 된 클래스에 대한 코드의 내가 배열을 채운 다음 시도하고 그것을 통해 반복 코드입니다.
public void HeadToHead(){
try
{
//Sets up the connedtion to the database and installs drivers which are required.
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost", "username", "password");
String SQL = "SELECT * FROM PreviousSeasons WHERE HomeTeam=? and AwayTeam=?";
PreparedStatement prepst;
prepst = con.prepareStatement(SQL);
prepst.setString(1,box1.getSelectedItem().toString());
prepst.setString(2,box2.getSelectedItem().toString());
rs = prepst.executeQuery();
while (rs.next())
{
//This retrieves each row of League table and adds it to an array in the League Results class.
hometeam = rs.getString("HomeTeam");
awayteam = rs.getString("AwayTeam");
result = rs.getString("Result");
custs = (hometeam + "," + awayteam + "," + result); // Takes all the variables containging a single customers information and puts it into a string, seperated by commas.
SeasonResults.add(hometeam, awayteam, result);
}
}
catch (Exception e)
{
System.out.println("Error " +e);
}
Seasonrecord = SeasonResults.getCollection();
seasons = new SeasonResults[Seasonrecord.size()];
Iterator iterateSeason = Seasonrecord.iterator();
int i = 0;
while(iterateSeason.hasNext()){
seasons[i] = (SeasonResults)iterateSeason.next();
i++;
if(result.equals("HW")){
hometeamvalue = hometeamvalue + 50;
}
else if(result.equals("D")){
hometeamvalue = hometeamvalue + 10;
awayteamvalue = awayteamvalue + 10;
}
else{
if(result.equals("AW")){
awayteamvalue = awayteamvalue + 50;
}
}
}
}
데이터베이스에 5 개의 '결과'필드가 있습니다. 2는 'HW', 2는 'AW', 1은 'D'입니다. 내가하려고하는 것은 'hometeamvalue'와 'awayteamvalue'를 출력하는 것이지만, 그 값은 단지 10으로 인쇄됩니다. 첫 번째 필드의 값만 사용됩니다.
결과를 GUI에 표시하려고 할 때 동일한 코드를 사용하여 배열을 반복합니다. 모든 필드가 표시됩니다. 그러나 그들과 계산을 시도 할 때 작동하지 않습니다.
어떤 아이디어가 문제입니까?
경우 얼굴 알려줘 result.equals ("HW"))는 seasons [i] .getResult(). equals ("HW")로 결과로 대체하고 나머지 두 개의 if 문도 마찬가지입니다. 시도했지만 – Naren
시도했지만 작동하지 않습니다. 배열의 첫 번째 항목에 대해서는 문제가 없으므로 그 부분이 문제라고 생각하지 않지만 더 이상 가지 않고 다른 항목을 가져 오지는 않습니다. – user3139748
그것은 작동하지만 U ur Serachesults 코드도 변경해야합니다 .. SeasonResults.java를 전적으로 게시 할 수 있으며 같은 클래스의 개인 정적지도 allResults를 가져 오는 이유는 무엇입니까? –
Naren