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());
}

}



















}