first commit
This commit is contained in:
96
TP_DEV3.1/Base de donnée/Vote.java
Normal file
96
TP_DEV3.1/Base de donnée/Vote.java
Normal file
@@ -0,0 +1,96 @@
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
|
||||
import org.mariadb.jdbc.*;
|
||||
|
||||
public class Vote {
|
||||
|
||||
private static final String lien = "jdbc:mariadb://dwarves.iut-fbleau.fr/yolou";
|
||||
private static final String user = "yolou";
|
||||
private static final String mdp = "serikhanejunior";
|
||||
|
||||
public static void main(String[] args) {
|
||||
|
||||
|
||||
|
||||
|
||||
if(args.length !=1 ){
|
||||
|
||||
System.err.println("Met un seul pays ");
|
||||
System.exit(0);
|
||||
|
||||
|
||||
}
|
||||
|
||||
String pays = args[0];
|
||||
|
||||
try {
|
||||
|
||||
Class.forName("org.mariadb.jdbc.Driver");
|
||||
}catch(ClassNotFoundException e) {
|
||||
|
||||
System.err.println("Il y'a pas de DriverManager Maria DB");
|
||||
}
|
||||
|
||||
String requete_sql = "Select pays_votant as votant, p.points" +
|
||||
"FROM Points p " +
|
||||
"JOIN Competiteur c ON p.competiteur_id = c.id " +
|
||||
"JOIN Votant v ON p.votant_id = v.id " +
|
||||
"WHERE c.pays = ? " +
|
||||
"ORDER BY v.pays_votant";
|
||||
|
||||
String totalSql =
|
||||
"SELECT SUM(p.points) " +
|
||||
"FROM Points p " +
|
||||
"JOIN Competiteur c ON p.competiteur_id = c.id " +
|
||||
"WHERE c.pays = ?";
|
||||
|
||||
Connection cnx = DriverManager.getConnection(lien, user, mdp);
|
||||
PreparedStatement pst = cnx.prepareStatement(requete_sql);
|
||||
PreparedStatement pstTotal = cnx.prepareStatement(totalSql);
|
||||
|
||||
pst.setString(1, pays);
|
||||
try (ResultSet rs = pst.executeQuery()) {
|
||||
while (rs.next()) {
|
||||
String votant = rs.getString("votant");
|
||||
int pts = rs.getInt("points");
|
||||
System.out.printf("%-10s %d%n", votant, pts);
|
||||
}
|
||||
}
|
||||
|
||||
pstTotal.setString(1, pays);
|
||||
try (ResultSet rsT = pstTotal.executeQuery()) {
|
||||
rsT.next();
|
||||
int total = rsT.getInt(1);
|
||||
System.out.println("---------");
|
||||
System.out.println("Total " + total);
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Erreur SQL : " + e.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
37
TP_DEV3.1/Base de donnée/sql.txt
Normal file
37
TP_DEV3.1/Base de donnée/sql.txt
Normal file
@@ -0,0 +1,37 @@
|
||||
-- Table des pays
|
||||
|
||||
CREATE TABLE Pays_vote (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
nom VARCHAR(50) UNIQUE NOT NULL
|
||||
);
|
||||
|
||||
-- Table des votes
|
||||
|
||||
CREATE TABLE Vote (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
paysfrom_id INT NOT NULL, -- pays qui vote
|
||||
paysto_id INT NOT NULL, -- pays qui reçoit
|
||||
nb_points INT NOT NULL,
|
||||
FOREIGN KEY (paysfrom_id) REFERENCES Pays_vote(id),
|
||||
FOREIGN KEY (paysto_id) REFERENCES Pays_vote(id)
|
||||
);
|
||||
|
||||
-- Insertion des pays
|
||||
insert into Pays_vote (nom) values ('Pays-Bas'), ('Italie'), ('Russie');
|
||||
|
||||
-- Récupération automatique des id (supposons)
|
||||
-- Pays-Bas = 1, Italie = 2, Russie = 3
|
||||
|
||||
-- Insertion des votes (avec id)
|
||||
VALUES
|
||||
(2, 1, 5), -- Italie → Pays-Bas = 5
|
||||
(3, 1, 5), -- Russie → Pays-Bas = 5
|
||||
(1, 2, 16), -- Pays-Bas → Italie = 16
|
||||
(3, 2, 1), -- Russie → Italie = 1
|
||||
(1, 3, 5), -- Pays-Bas → Russie = 5
|
||||
(2, 3, 8); -- Italie → Russie = 8
|
||||
|
||||
Insert into Vote (paysfrom_id,paysto_id,nb_points) values (2,1,5), (3,1,5), (1,2,16), (3,2,1), (1,3,5), (2,3,8);
|
||||
|
||||
javac -cp .:/export/documents/mariadb-client.jar Vote.java
|
||||
java -cp .:/export/documents/mariadb-client.jar:. Vote Italie
|
||||
Reference in New Issue
Block a user