package com.insert.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 { Component c; Container con; 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(); //quitter.addActionListener (new ListenerQuitter(this)); //c.setColor (Color yellow); // con.setFont(Font Courier,Font.BOLD, 50); 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); // } //quitter.addWindowListener (new WindowAdapter() // public void windowClosing(WindowEvent e) // { //System.exit(0); // } //}); } public void actionPerformed(ActionEvent e) { if (e.getSource().equals(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, telephone ,nbre_plan) VALUES ( '" + num.getText() + "' '" + nom.getText() + "' '" + adresse.getText() + "' '" + cp.getText() + "' '" + ville.getText() + "' '" + tel.getText() + "' '" + plan.getText() + "' )"); String num = ""; String nom = ""; String adresse = ""; String cp = ""; String ville = ""; String tel = ""; String plan = ""; while (resultat.next()) { num += resultat.getString("num_client") + ""; nom += resultat.getString("nom du client") + ""; adresse += resultat.getString("adresse") + ""; cp += resultat.getString("cp") + ""; ville += resultat.getString("ville") + ""; tel += resultat.getString("telephone") + ""; plan += resultat.getString("nbre_plan") + ""; } JOptionPane.showMessageDialog(null, "<html>" +"<center> Test " +"\" +\"---- <li>Test2</li>, \" +" " +"</center>" +"</html>"); //"Client :" + nom + " 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[] args) { Frame f = new Inserer(); } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre question