add Dev Part

This commit is contained in:
2025-09-26 08:56:23 +02:00
parent e2d2e17407
commit 96b3c953f8
13 changed files with 266 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
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;
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 ResultSet 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"
);
ResultSet result = query.executeQuery();
try {
query.close();
} catch(SQLException exception) {
System.err.println("Error while trying to close the query.");
}
return result;
} catch(SQLException exception) {
System.err.println("Error while trying to prepare or execute the query.");
}
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,26 @@
import java.sql.ResultSet;
import java.sql.SQLException;
public class Main {
public static void main(String[] args) {
Database database = new Database();
String country = "France";
ResultSet result = database.getVotesFromCompetitor(country);
try {
while(result.next()) {
System.out.println(country + " -> " + result.getString("name") + " : " + result.getInt("points"));
}
} catch(SQLException exception) {
System.err.println("Error while read the data from the result.");
}
try {
result.close();
} catch(SQLException exception) {
System.err.println("Error while trying to close the result.");
}
}
}

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,2 @@
javac Main.java;
java -cp "/export/documents/mariadb-client.jar" Main;