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 146
| import java.awt.Container;
import java.awt.event.*;
import java.net.URL;
import java.sql.*;
import java.util.*;
import javax.swing.*;
public class Add extends JFrame implements ActionListener {
private JDesktopPane bureau;
private Container contenu;
private JMenuBar menu;
private JMenu fich;
private JMenuItem quit;
private JLabel labnomE;
private JLabel labrueE;
private JLabel labvilleE;
private JLabel labcodePostE;
private JLabel labnumE;
private JLabel labnomResp;
private JLabel labnomInterloc;
private JTextField txtnomE;
private JTextField txtrueE;
private JTextField txtvilleE;
private JTextField txtcodePostE;
private JTextField txtnumE;
private JTextField txtnomResp;
private JTextField txtnomInterloc;
private JButton envoie;
private String rqt;
private Connection con;
public Add()
{
super("Ajout d'un etablissement");
this.bureau = new JDesktopPane();
this.menu = new JMenuBar();
this.fich = new JMenu("Menu");
this.menu.add(fich);
this.quit = new JMenuItem("Quitter");
this.fich.add(quit);
this.setJMenuBar(menu);
this.getContentPane().add(bureau);
this.quit.addActionListener(this);
this.labnomE = new JLabel("Nom etablissement ");
this.labrueE = new JLabel("Rue ");
this.labvilleE = new JLabel("Ville ");
this.labcodePostE = new JLabel("Code Postal ");
this.labnumE = new JLabel("Numero telephone ");
this.labnomResp = new JLabel("Nom du responsable");
this.labnomInterloc = new JLabel("Nom interlocuteur ");
this.txtnomE = new JTextField("");
this.txtrueE = new JTextField("");
this.txtvilleE = new JTextField("");
this.txtcodePostE = new JTextField("");
this.txtnumE = new JTextField("");
this.txtnomResp = new JTextField("");
this.txtnomInterloc = new JTextField("");
this.envoie = new JButton("Envoyer");
this.envoie.addActionListener(this);
this.contenu = this.getContentPane();
this.labnomE.setBounds(100,40,120,20);
this.labrueE.setBounds(100,70,120,20);
this.labvilleE.setBounds(100,100,120,20);
this.labcodePostE.setBounds(100,130,120,20);
this.labnumE.setBounds(100,160,120,20);
this.labnomResp.setBounds(100,190,120,20);
this.labnomInterloc.setBounds(100,220,120,20);
this.txtnomE.setBounds(280,40,120,20);
this.txtrueE.setBounds(280,70,120,20);
this.txtvilleE.setBounds(280,100,120,20);
this.txtcodePostE.setBounds(280,130,120,20);
this.txtnumE.setBounds(280,160,120,20);
this.txtnomResp.setBounds(280,190,120,20);
this.txtnomInterloc.setBounds(280,220,120,20);
this.envoie.setBounds(190,300,120,20);
this.contenu.setLayout(null);
this.contenu.add(labnomE);
this.contenu.add(labrueE);
this.contenu.add(labnumE);
this.contenu.add(labvilleE);
this.contenu.add(labcodePostE);
this.contenu.add(labnomResp);
this.contenu.add(labnomInterloc);
this.contenu.add(txtnomE);
this.contenu.add(txtrueE);
this.contenu.add(txtvilleE);
this.contenu.add(txtcodePostE);
this.contenu.add(txtnumE);
this.contenu.add(txtnomResp);
this.contenu.add(txtnomInterloc);
this.contenu.add(envoie);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == quit)
{System.exit(0);}
if (e.getSource() == envoie) {
// 1. def du driver
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException except)
{ JOptionPane.showMessageDialog(this, "Erreur de drivers : " + except);}
// 2. creation d'une connexion a la base de donnees
String url = "jdbc:mysql://localhost/axis";
String user = "root";
String password = "";
try {
con = DriverManager.getConnection(url, user, password);
}
catch (SQLException except1){
JOptionPane.showMessageDialog(this,"Erreur de connexion a la base : " + except1);
}
// 3. creation de la requete
try {
Statement stmt = con.createStatement();
rqt = "INSERT INTO etablissement(nomE, rueE, villeE, codePost, numTel, nomResp, nomInterloc)" +
"VALUES ("+txtnomE.getText()+","+txtrueE.getText()+","+txtvilleE.getText()+","+txtcodePostE.getText()+"," +
""+txtnumE.getText()+","+txtnomInterloc.getText()+");";
// 4. envoie de la requete
int nbLignSupp = stmt.executeUpdate(rqt);
JOptionPane.showMessageDialog(this,"Nombre de lignes supprimees : "+nbLignSupp);
}
catch (SQLException except2) {
JOptionPane.showMessageDialog(this,"Erreur sur la requete : " + except2);
}
}
} |
Partager