1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
| package reseau;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class FenetreRecupererReseau extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
ArrayList<Integer> liste=new ArrayList<Integer>();
public ArrayList<Integer> connexion(){
Connection connexion;
Statement statement;
ResultSet resultat ;
try{
Class.forName("com.mysql.jdbc.Driver");
connexion=DriverManager.getConnection("jdbc:mysql://localhost/memoire","root","boutheina");
statement = connexion.createStatement();
String sql="select reseau from connectivite group by reseau";
resultat=statement.executeQuery(sql);
while(resultat.next()){
int x=resultat.getInt("reseau");
liste.add(x);
}
}
catch(Exception e){
e.printStackTrace();
System.out.println("Impossible de se connecter à MySQL");
}
return liste;
}
/**
*
*/
private JPanel container = new JPanel();
private JComboBox combo = new JComboBox();
private JLabel label = new JLabel("Une ComboBox");
JPanel top = new JPanel();
public FenetreRecupererReseau(){
this.setTitle("Animation");
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocationRelativeTo(null);
container.setBackground(Color.white);
container.setLayout(new BorderLayout());
ArrayList<Integer>a=connexion();
for(int i=0;i<a.size();i++){
int x=a.get(i);
combo.addItem(x);
}
combo.addActionListener(new ItemAction());
combo.setPreferredSize(new Dimension(100,20));
combo.setForeground(Color.blue);
top.add(label);
top.add(combo);
container.add(top, BorderLayout.NORTH);
this.setContentPane(container);
this.setVisible(true);
}
/**
* Classe interne implémentant l'interface ItemListener
*/
class ItemAction implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("ActionListener : action sur " + combo.getSelectedItem());
String s=combo.getSelectedItem().toString();
int x=Integer.parseInt(s);
Connection connexion;
Statement statement;
ResultSet resultat ;
try{
Class.forName("com.mysql.jdbc.Driver");
connexion=DriverManager.getConnection("jdbc:mysql://localhost/memoire","root","boutheina");
statement = connexion.createStatement();
String sql="select max(nbreAmis),min(nbreAmis),avg(nbreAmis) from connectivite where reseau="+x;
System.out.println(sql);
resultat=statement.executeQuery(sql);
while(resultat.next()){
int max=resultat.getInt(1);
int min=resultat.getInt(2);
int avg=resultat.getInt(3);
System.out.println(max);
System.out.println(min);
System.out.println(avg);
JLabel b= new JLabel("xxxx");
top.add(b);
JLabel l=new JLabel(" "+max+" "+min+" "+avg);
l.setVisible(true);
top.add(l);
}
}
catch(Exception ex){
ex.printStackTrace();
System.out.println("Impossible de se connecter à MySQL");
}
}
}
} |