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
|
try {
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost/mabase","root","");
statement = connection.createStatement();
result = statement.executeQuery("SELECT types_pdt FROM Cat1");
while(result.next())
{
dlm.addElement(result.getString("types_pdt"));
}
list.setModel(dlm);
list.setSelectedIndex(0);
list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
JPanel pan5 = new JPanel();
pan5.setBackground(Color.orange);
pan5.add(tab, BorderLayout.CENTER);
JPanel pan6 = new JPanel();
pan6.setBackground(Color.cyan);
JPanel pan7 = new JPanel();
pan7.setBackground(Color.GREEN);
pan7.add(lab);
pan7.add(list);
split2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, pan7, pan5);
split1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT, split2, pan6);
this.getContentPane().add(split1, BorderLayout.CENTER);
}catch (Exception e) {
e.printStackTrace();
}
list.addListSelectionListener(new AccesInfosPdt());
this.setVisible(true);
}
class AccesInfosPdt implements ListSelectionListener{
public void valueChanged(ListSelectionEvent arg0) {
if (!arg0.getValueIsAdjusting()) {
String pdt = (String) list.getSelectedValue();
System.out.println("Choix : " +pdt); //pour tester la sortie
try {
Class.forName("com.mysql.cj.jdbc.Driver").newInstance();
connection = DriverManager.getConnection("jdbc:mysql://localhost/mabase","root","");
statement = connection.createStatement();
result = statement.executeQuery("select Shops, Prix from ' "+ pdt +" ' ");
String columns[] = {"Shops","Prix"};
String data[][] = new String[4][2];
int i = 0;
while(result.next()) {
String shop = result.getString("Shops");
int prix = result.getInt("Prix");
data[i][0]= shop;
data[i][1] = prix + "";
i++;
}
tab = new JTable(data, columns);
tab.setShowGrid(false);
tab.setRowHeight(30);
tab.setTableHeader(null);
}catch (Exception e) {
e.printStackTrace();
}
}
}
}
} |
Partager