all (flemme)

This commit is contained in:
2026-02-10 17:46:27 +01:00
parent ef6ca77dc6
commit e8a6bb9959
64 changed files with 1824 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
public class Database {
private Connection connection;
public Database() {
try {
Class.forName("org.mariadb.jdbc.Driver");
this.connection = DriverManager.getConnection(
"jdbc:mariadb://dwarves.iut-fbleau.fr/baudrier",
"baudrier",
"baudrier"
);
} catch(ClassNotFoundException exception) {
System.err.println("Class 'org.mariadb.jdbc.Driver' not found.");
} catch(SQLException exception) {
System.err.println("Error while trying to connect to the database");
}
}
public Votes getVotesFromCompetitor(String country) {
try {
PreparedStatement query = this.connection.prepareStatement(
"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<Vote> voteList = new ArrayList<>();
while(result.next()) {
voteList.add(new Vote(result.getString("name"), result.getInt("points")));
}
try {
query.close();
} catch(SQLException exception) {
System.err.println("Error while trying to close the query.");
}
return new Votes(country, voteList);
} catch(SQLException exception) {
System.err.println("Error while trying to prepare or execute the query (" + exception.getMessage() + ").");
}
return null;
}
public void close() {
try {
this.connection.close();
} catch(SQLException exception) {
System.err.println("Error while trying to close the connection.");
}
}
}

View File

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

View File

@@ -0,0 +1,18 @@
public class Vote {
private String voter;
private int points;
public Vote(String voter, int points) {
this.voter = voter;
this.points = points;
}
public String getVoterName() {
return this.voter;
}
public int getPoints() {
return this.points;
}
}

View File

@@ -0,0 +1,30 @@
import java.util.ArrayList;
public class Votes {
private String competitor;
private ArrayList<Vote> voters;
public Votes(String competitor, ArrayList<Vote> voters) {
this.competitor = competitor;
this.voters = voters;
}
public String getCompetitorName() {
return this.competitor;
}
public ArrayList<Vote> getVoters() {
return this.voters;
}
public int getTotalPoints() {
int sum = 0;
for(Vote vote : this.voters) {
sum += vote.getPoints();
}
return sum;
}
}

View File

@@ -0,0 +1,23 @@
CREATE TABLE Countries (
id INT PRIMARY KEY,
name VARCHAR(30)
);
CREATE TABLE Votes (
id INT PRIMARY KEY AUTO_INCREMENT,
competitor_id INT REFERENCES Countries(id),
voter_id INT REFERENCES Countries(id),
points INT NOT NULL,
UNIQUE (competitor_id, voter_id)
)
INSERT INTO Countries VALUES (1, "France");
INSERT INTO Countries VALUES (2, "Pays-Bas");
INSERT INTO Countries VALUES (3, "Albanie");
INSERT INTO Countries VALUES (4, "Italie");
INSERT INTO Countries VALUES (5, "Russie");
INSERT INTO Votes VALUES (2, 4, 5);
INSERT INTO Votes VALUES (2, 5, 5);
INSERT INTO Votes VALUES (4, 2, 16);
INSERT INTO Votes VALUES (5, 2, 5);
INSERT INTO Votes VALUES (5, 4, 8);

View File

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