package com.inserer.test;
import java.awt.event.*;
import java.sql.*;
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
import javax.swing.*;
public class Inserer extends Frame implements ActionListener
{
Frame f = new Frame("exercice");
TextField num = new TextField("Numero du client", 20);
TextField nom = new TextField("Nom du client", 20);
TextField adresse = new TextField("Adresse", 20);
TextField cp = new TextField("Code postal", 20);
TextField ville = new TextField("Ville", 20);
TextField tel = new TextField("Telephone", 20);
TextField plan = new TextField("Nombre de plan", 20);
Label message = new Label();
Button quitter = new Button("QUITTER L'APPLICATION");
Button inserer = new Button("INSERER ");
Panel panneau = new Panel();
public Inserer() {
num.addActionListener(this);
nom.addActionListener(this);
adresse.addActionListener(this);
cp.addActionListener(this);
ville.addActionListener(this);
tel.addActionListener(this);
plan.addActionListener(this);
inserer.addActionListener(this);
num.selectAll(); // pour sélectionner le texte du champ
nom.selectAll();
adresse.selectAll();
cp.selectAll();
ville.selectAll();
tel.selectAll();
plan.selectAll();
setLayout(new FlowLayout());
add(num);
add(nom);
add(adresse);
add(cp);
add(ville);
add(tel);
add(plan);
add(quitter);
add(inserer);
setBounds(40, 60, 300, 200);
setTitle("INSERER UN NOUVEAU CLIENT");
setSize(300, 350);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource()== inserer)
//if (e.getSource() == num)
//if (e.getSource() == nom)
//if (e.getSource() == adresse)
//if (e.getSource() == cp)
//if (e.getSource() == ville)
//if (e.getSource() == tel)
//if (e.getSource() == plan)
{
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();
int res = instruction.executeUpdate
("INSERT INTO client(num_client, nom, adresse, cp, ville, tel, nbre_plan) VALUES ( " +
num.getText() + ", " + nom.getText() +
" , " + adresse.getText() +
" , " + cp.getText() +
" , " + ville.getText() +
" , " + tel.getText() +
" , " + plan.getText() + " )");
JOptionPane.showMessageDialog(null,
"Client :" + nom.getText() + " ajouté avec succès");
//"\u20ac");
} 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[] arg) {
Frame f = new Inserer();
}
}