DEV 3.1 DB
This commit is contained in:
parent
1c0439f7c0
commit
6b8527e3c8
BIN
DEV 3.1/Victoire/RefreshListener.class
Normal file
BIN
DEV 3.1/Victoire/RefreshListener.class
Normal file
Binary file not shown.
56
DEV 3.1/Victoire/RefreshListener.java
Normal file
56
DEV 3.1/Victoire/RefreshListener.java
Normal file
@ -0,0 +1,56 @@
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
import java.sql.*;
|
||||
|
||||
public class RefreshListener implements ActionListener {
|
||||
public RefreshListener() {
|
||||
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 Competiteur, SUM(Points) FROM Vote GROUP BY Competiteur;");
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
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 void actionPerformed(ActionEvent e) {
|
||||
Victoire.refresh();
|
||||
}
|
||||
}
|
BIN
DEV 3.1/Victoire/Victoire.class
Normal file
BIN
DEV 3.1/Victoire/Victoire.class
Normal file
Binary file not shown.
33
DEV 3.1/Victoire/Victoire.java
Normal file
33
DEV 3.1/Victoire/Victoire.java
Normal file
@ -0,0 +1,33 @@
|
||||
import java.sql.*;
|
||||
import javax.swing.*;
|
||||
import java.awt.*;
|
||||
|
||||
public class Victoire {
|
||||
|
||||
public static JLabel scoreLabel;
|
||||
public static JLabel countryLabel;
|
||||
|
||||
public static void refresh() {
|
||||
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
BIN
DEV 3.1/Vote/Vote.class
Normal file
BIN
DEV 3.1/Vote/Vote.class
Normal file
Binary file not shown.
58
DEV 3.1/Vote/Vote.java
Normal file
58
DEV 3.1/Vote/Vote.java
Normal file
@ -0,0 +1,58 @@
|
||||
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.");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user