DEV TP01
This commit is contained in:
@@ -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.");
|
||||
}
|
||||
|
Reference in New Issue
Block a user