This commit is contained in:
2025-09-26 13:21:58 +02:00
parent 7c72e94d8d
commit e35f36db4b
4 changed files with 65 additions and 28 deletions

View File

@@ -3,17 +3,9 @@ import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class Database {
//Countries
public static String COUNTRY_ID = "id";
public static String COUNTRY_NAME = "name";
//Votes table
public static String VOTES_COMPETITOR_ID = "competitor_id";
public static String VOTES_VOTER_ID = "voter_id";
public static String VOTES_POINTS = "points";
private Connection connection;
@@ -33,13 +25,20 @@ public class Database {
}
}
public ResultSet getVotesFromCompetitor(String country) {
public Votes getVotesFromCompetitor(String country) {
try {
PreparedStatement query = this.connection.prepareStatement(
"SELECT Countries.name, Countries.points FROM Votes INNER JOIN Countries ON Votes.competitor_id = Countries.id WHERE Votes.competitor_id = Countries.id"
"SELECT Countries.name Votes.points" +
"FROM Votes JOIN Countries ON Votes.competitor_id = Countries.id" +
"WHERE Countries.name = " + country
);
ResultSet result = query.executeQuery();
ArrayList<Vote> voteList = new ArrayList<>();
while(result.next()) {
voteList.add(new Vote(result.getString("name"), result.getInt("points")));
}
try {
query.close();
@@ -47,7 +46,7 @@ public class Database {
System.err.println("Error while trying to close the query.");
}
return result;
return new Votes(country, voteList);
} catch(SQLException exception) {
System.err.println("Error while trying to prepare or execute the query.");
}