From 8b6d447574bb162d2409eb555e5285a41119fddd Mon Sep 17 00:00:00 2001 From: Nathan Baudrier Date: Thu, 2 Oct 2025 10:47:14 +0200 Subject: [PATCH] add a model to manage data --- DEV/DEV3.1/TP01/Exercise1/Database.java | 11 +++++++---- DEV/DEV3.1/TP01/Exercise1/Main.java | 4 +++- DEV/DEV3.1/TP01/Exercise1/start.sh | 3 ++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/DEV/DEV3.1/TP01/Exercise1/Database.java b/DEV/DEV3.1/TP01/Exercise1/Database.java index ba75b3c..7757143 100644 --- a/DEV/DEV3.1/TP01/Exercise1/Database.java +++ b/DEV/DEV3.1/TP01/Exercise1/Database.java @@ -28,10 +28,13 @@ public class Database { public Votes getVotesFromCompetitor(String country) { try { PreparedStatement query = this.connection.prepareStatement( - "SELECT Countries.name Votes.points" + - "FROM Votes JOIN Countries ON Votes.competitor_id = Countries.id" + - "WHERE Countries.name = " + country + "SELECT Voters.name, Votes.points " + + "FROM Votes " + + "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(); ArrayList voteList = new ArrayList<>(); @@ -48,7 +51,7 @@ public class Database { return new Votes(country, voteList); } 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; diff --git a/DEV/DEV3.1/TP01/Exercise1/Main.java b/DEV/DEV3.1/TP01/Exercise1/Main.java index 938cc46..a9b7ede 100644 --- a/DEV/DEV3.1/TP01/Exercise1/Main.java +++ b/DEV/DEV3.1/TP01/Exercise1/Main.java @@ -3,7 +3,7 @@ public class Main { public static void main(String[] args) { Database database = new Database(); - Votes votesForItaly = database.getVotesFromCompetitor("Italie"); + Votes votesForItaly = database.getVotesFromCompetitor("Pays-Bas"); System.out.println("Vote " + votesForItaly.getCompetitorName() + " :"); for(Vote vote : votesForItaly.getVoters()) { @@ -12,5 +12,7 @@ public class Main { System.out.println("-------------------"); System.out.println("Total " + votesForItaly.getTotalPoints()); + + database.close(); } } diff --git a/DEV/DEV3.1/TP01/Exercise1/start.sh b/DEV/DEV3.1/TP01/Exercise1/start.sh index 884b328..8d694b7 100755 --- a/DEV/DEV3.1/TP01/Exercise1/start.sh +++ b/DEV/DEV3.1/TP01/Exercise1/start.sh @@ -1,2 +1,3 @@ +rm -rf *.class javac Main.java; -java -cp "/export/documents/mariadb-client.jar" Main; \ No newline at end of file +java -cp ".:/export/documents/mariadb-client.jar" Main; \ No newline at end of file