package com.maj.test;
import java.awt. * ;
import java.awt.event. * ;
import java.sql.*;
import javax.swing.JOptionPane;
public class Maj extends Frame implements ActionListener{
Button Bout1 = new Button("Ajouter");
Panel Panel1 = new Panel();
TextField Edit1 = new TextField("Numero du client");
TextArea Memo1 = new TextArea("Resultat de la requete");
public Maj() {
this.setBounds(80, 100, 400, 250);
this.setTitle("Un bouton avec Frame");
this.setBackground(Color.orange);
Panel1.setBounds(10, 40, 180, 100);
Panel1.setBackground(Color.red);
Panel1.setLayout(null);
Bout1.setBounds(5, 10, 60, 30);
Edit1.setBounds(15, 160, 200, 25);
Memo1.setBounds(230, 145, 150, 100);
Panel1.add(Bout1);
this.setLayout(null);
this.add(Panel1);
this.add(Edit1);
this.add(Memo1);
Edit1.addActionListener(this);
Edit1.selectAll(); // pour sélectionner le texte du champ
setSize(150, 250);
setVisible(true);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == Edit1) {
Connection con = null;
Statement instruction = null;
ResultSet resultat = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/techniclim",
"root", "");
instruction = con.createStatement();
resultat = instruction.executeQuery(
"SELECT num_client,nom,adresse,cp,ville,tel, nbre_plan FROM client WHERE num_client = '"+Edit1.getText()+"'");
String produit = "";
String produit2 = "";
String produit3 = "";
String produit4 = "";
String produit5 = "";
String produit6 = "";
String produit7 = "";
while (resultat.next()) {
produit += resultat.getString("num_client") + "";
produit2 += resultat.getString("nom") + "";
produit3 += resultat.getString("adresse") + "";
produit4 += resultat.getString("cp") + "";
produit5 += resultat.getString("ville") + "";
produit6 += resultat.getString("tel") + "";
produit7 += resultat.getString("nbre_plan") + "";
}
JOptionPane.showMessageDialog(null,
"nom : " + produit2 + "adresse:" +produit3+ "cp :" +produit4+ "ville : " + produit5+ "tel :" +produit6+ "nbre_plan : "+produit7+ "");
}
catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(null,
"Classe introuvable" + ex.getMessage());
}
catch (SQLException ex) {
JOptionPane.showMessageDialog(null,
"erreur JDBC : " + ex.getMessage());
}
finally {
try {
if (resultat != null)
resultat.close();
if (con != null)
con.close();
}
catch (SQLException ex) {
ex.printStackTrace();
System.exit(0);
}
}
}
}
public static void main(String[] args) {
Frame f = new Maj();
}
}