APL/DEV 3.1/TP1/Victoire/Victoire.java

83 lines
2.4 KiB
Java
Raw Normal View History

2022-09-07 11:16:04 +02:00
import java.sql.*;
2022-09-07 15:35:41 +02:00
import javax.swing.*;
import java.awt.*;
2022-09-07 11:16:04 +02:00
2022-09-07 15:35:41 +02:00
public class Victoire {
public static JLabel scoreLabel;
public static JLabel countryLabel;
public static void refresh() {
2022-09-07 11:16:04 +02:00
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 {
2022-09-07 15:35:41 +02:00
PreparedStatement req = cnx.prepareStatement("SELECT Nom, SUM(Points) FROM Vote LEFT JOIN Pays ON Vote.Competiteur = Pays.ID GROUP BY Nom;");
2022-09-07 11:16:04 +02:00
req.executeUpdate();
int maxPoints = 0;
2022-09-07 15:35:41 +02:00
String country = "";
2022-09-07 11:16:04 +02:00
ResultSet rs = req.executeQuery();
while (rs.next()) {
int score = rs.getInt(2);
if (score > maxPoints) {
country = rs.getString(1);
maxPoints = score;
}
}
2022-09-07 15:35:41 +02:00
if (country != "") {
countryLabel.setText(country);
scoreLabel.setText(Integer.toString(maxPoints));
}
2022-09-07 11:16:04 +02:00
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.");
}
}
2022-09-07 15:35:41 +02:00
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);
2022-09-07 11:16:04 +02:00
Victoire.refresh();
}
2022-09-07 15:35:41 +02:00
}