APL/DEV 3.1/Vote/Vote.java

58 lines
1.6 KiB
Java
Raw Normal View History

2022-09-07 11:16:04 +02:00
import java.sql.*;
public class Vote {
public static void main(String[] args) {
if (args.length < 1) {
System.err.println("No country given.");
return;
}
try {
Class.forName("org.mariadb.jdbc.Driver");
} catch (ClassNotFoundException ex) {
System.err.println("MariaDB driver not found");
return;
}
Connection cnx;
try {
cnx = DriverManager.getConnection("jdbc:mariadb://dwarves.iut-fbleau.fr/horville", "horville", "a5gc95");
} catch (SQLException ex) {
System.err.println("Unable to access Database.");
return;
}
try {
PreparedStatement req = cnx.prepareStatement("SELECT Votants, Points FROM Vote WHERE Competiteur = (?);");
req.setString(1, args[0]);
req.executeUpdate();
int total = 0;
ResultSet rs = req.executeQuery();
while (rs.next()) {
String country = rs.getString(1);
int score = rs.getInt(2);
total += score;
System.out.println(country + " " + score);
}
System.out.println(" ---");
System.out.println("Total " + total);
rs.close();
req.close();
} catch (SQLException ex) {
System.err.println("SQL Request exception.");
System.err.println (ex);
}
try {
cnx.close();
} catch (SQLException ex) {
System.err.println("Unable to close connection.");
}
}
}