add a model to manage data

This commit is contained in:
2025-10-02 10:47:14 +02:00
parent e35f36db4b
commit 8b6d447574
3 changed files with 12 additions and 6 deletions

View File

@@ -28,10 +28,13 @@ public class Database {
public Votes getVotesFromCompetitor(String country) { public Votes getVotesFromCompetitor(String country) {
try { try {
PreparedStatement query = this.connection.prepareStatement( PreparedStatement query = this.connection.prepareStatement(
"SELECT Countries.name Votes.points" + "SELECT Voters.name, Votes.points " +
"FROM Votes JOIN Countries ON Votes.competitor_id = Countries.id" + "FROM Votes " +
"WHERE Countries.name = " + country "JOIN Countries AS Competitors ON Votes.competitor_id = Competitors.id " +
"JOIN Countries AS Voters ON Votes.voter_id = Voters.id " +
"WHERE Competitors.name = ?;"
); );
query.setString(1, country);
ResultSet result = query.executeQuery(); ResultSet result = query.executeQuery();
ArrayList<Vote> voteList = new ArrayList<>(); ArrayList<Vote> voteList = new ArrayList<>();
@@ -48,7 +51,7 @@ public class Database {
return new Votes(country, voteList); return new Votes(country, voteList);
} catch(SQLException exception) { } catch(SQLException exception) {
System.err.println("Error while trying to prepare or execute the query."); System.err.println("Error while trying to prepare or execute the query (" + exception.getMessage() + ").");
} }
return null; return null;

View File

@@ -3,7 +3,7 @@ public class Main {
public static void main(String[] args) { public static void main(String[] args) {
Database database = new Database(); Database database = new Database();
Votes votesForItaly = database.getVotesFromCompetitor("Italie"); Votes votesForItaly = database.getVotesFromCompetitor("Pays-Bas");
System.out.println("Vote " + votesForItaly.getCompetitorName() + " :"); System.out.println("Vote " + votesForItaly.getCompetitorName() + " :");
for(Vote vote : votesForItaly.getVoters()) { for(Vote vote : votesForItaly.getVoters()) {
@@ -12,5 +12,7 @@ public class Main {
System.out.println("-------------------"); System.out.println("-------------------");
System.out.println("Total " + votesForItaly.getTotalPoints()); System.out.println("Total " + votesForItaly.getTotalPoints());
database.close();
} }
} }

View File

@@ -1,2 +1,3 @@
rm -rf *.class
javac Main.java; javac Main.java;
java -cp "/export/documents/mariadb-client.jar" Main; java -cp ".:/export/documents/mariadb-client.jar" Main;