APL/DEV 3.1/TP1/Victoire/Victoire.java
2022-09-07 15:35:41 +02:00

83 lines
2.4 KiB
Java

import java.sql.*;
import javax.swing.*;
import java.awt.*;
public class Victoire {
public static JLabel scoreLabel;
public static JLabel countryLabel;
public static void refresh() {
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 Nom, SUM(Points) FROM Vote LEFT JOIN Pays ON Vote.Competiteur = Pays.ID GROUP BY Nom;");
req.executeUpdate();
int maxPoints = 0;
String country = "";
ResultSet rs = req.executeQuery();
while (rs.next()) {
int score = rs.getInt(2);
if (score > maxPoints) {
country = rs.getString(1);
maxPoints = score;
}
}
if (country != "") {
countryLabel.setText(country);
scoreLabel.setText(Integer.toString(maxPoints));
}
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.");
}
}
public static void main(String[] args) {
JFrame window = new JFrame("Victoire");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(300, 300);
window.setLocation(200, 200);
JButton refreshBT = new JButton("");
refreshBT.addActionListener(new RefreshListener());
window.add(refreshBT, BorderLayout.SOUTH);
window.setVisible(true);
countryLabel = new JLabel("...", JLabel.CENTER);
scoreLabel = new JLabel("0", JLabel.CENTER);
window.add(countryLabel, BorderLayout.NORTH);
window.add(scoreLabel, BorderLayout.CENTER);
Victoire.refresh();
}
}