Application

Fermé
papsirfa Messages postés 5 Date d'inscription mardi 14 juillet 2015 Statut Membre Dernière intervention 23 juillet 2015 - Modifié par KX le 15/07/2015 à 21:25
bonjour tout le monde voici mon code j'aimerais creer une application (ma première) sur netbeans genre formulaire et j'aimerais enregistrer mes information dans la base de données mysql mais ca passe pas svp quelle solution me proposer vous


/*
 * To change this license header, choose License Headers in Project Properties. 
 * To change this template file, choose Tools | Templates
 * and open the template in the editor. 
 */
package guimob;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/** 
 * @author TOSHIBA 
 */
public class Formulaire extends javax.swing.JFrame {
  Connection conn = new DBconnecte().connect();
  Statement st;
  PreparedStatement ps;

  /**
   * Creates new form Formulaire
   */
  public Formulaire() {
    initComponents();
  }

  /**
   * This method is called from within the constructor to initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is always
   * regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">
  private void initComponents() {
    jDateChooser1 = new com.toedter.calendar.JDateChooser();
    jButton1 = new javax.swing.JButton();
    jButton2 = new javax.swing.JButton();
    jButton3 = new javax.swing.JButton();
    jLabel9 = new javax.swing.JLabel();
    jPanel1 = new javax.swing.JPanel();
    jTadresse = new javax.swing.JTextField();
    jLabel3 = new javax.swing.JLabel();
    jLabel7 = new javax.swing.JLabel();
    jLabel4 = new javax.swing.JLabel();
    jLabel6 = new javax.swing.JLabel();
    jLabel8 = new javax.swing.JLabel();
    jTprofession = new javax.swing.JTextField();
    jComboBox1 = new javax.swing.JComboBox();
    jTlieu = new javax.swing.JTextField();
    jLabel1 = new javax.swing.JLabel();
    jDateChooser2 = new com.toedter.calendar.JDateChooser();
    jLabel5 = new javax.swing.JLabel();
    jTprenom = new javax.swing.JTextField();
    jTid = new javax.swing.JTextField();
    jTnom = new javax.swing.JTextField();
    jLabel2 = new javax.swing.JLabel();
    setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
    setPreferredSize(new java.awt.Dimension(600, 500));
    jButton1.setText("Ajouter");
    jButton1.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton1ActionPerformed(evt);
      }
    });
    jButton2.setText("Modifier");
    jButton3.setText("Quitter");
    jButton3.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jButton3ActionPerformed(evt);
      }
    });
    jLabel9.setFont(new java.awt.Font("Times New Roman", 0, 18));
    // NOI18N
    jLabel9.setText("Formulaire d'inscription");
    jTadresse.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jTadresseActionPerformed(evt);
      }
    });
    jLabel3.setText("Nom:");
    jLabel7.setText("Sexe:");
    jLabel4.setText("Adresse:");
    jLabel6.setText("Lieu de Naissance:");
    jLabel8.setText("Profession:");
    jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "M", "F" }));
    jTlieu.addActionListener(new java.awt.event.ActionListener() {
      public void actionPerformed(java.awt.event.ActionEvent evt) {
        jTlieuActionPerformed(evt);
      }
    });
    jLabel1.setText("IdMembre:");
    jLabel5.setText("Date de Naissance:");
    jLabel2.setText("Prenom:");
    javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
    jPanel1.setLayout(jPanel1Layout);
    jPanel1Layout
        .setHorizontalGroup(jPanel1Layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(
                jPanel1Layout
                    .createSequentialGroup()
                    .addContainerGap()
                    .addGroup(
                        jPanel1Layout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(
                                jPanel1Layout
                                    .createSequentialGroup()
                                    .addGroup(
                                        jPanel1Layout
                                            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel1)
                                            .addComponent(jLabel2)
                                            .addComponent(jLabel4)
                                            .addComponent(jLabel3)
                                            .addComponent(jLabel5))
                                    .addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        Short.MAX_VALUE)
                                    .addGroup(
                                        jPanel1Layout
                                            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(
                                                jPanel1Layout
                                                    .createParallelGroup(
                                                        javax.swing.GroupLayout.Alignment.LEADING,
                                                        false)
                                                    .addComponent(jTadresse)
                                                    .addComponent(
                                                        jTnom,
                                                        javax.swing.GroupLayout.Alignment.TRAILING)
                                                    .addComponent(jTprenom)
                                                    .addComponent(
                                                        jTid,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        117,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                                            .addComponent(
                                                jDateChooser2,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                137,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addGroup(
                                jPanel1Layout
                                    .createSequentialGroup()
                                    .addGroup(
                                        jPanel1Layout
                                            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel7)
                                            .addComponent(jLabel8)
                                            .addComponent(jLabel6))
                                    .addGap(120, 120, 120)
                                    .addGroup(
                                        jPanel1Layout
                                            .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(
                                                jComboBox1,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                117,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(
                                                jTlieu,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                117,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                            .addComponent(
                                                jTprofession,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                117,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGap(20, 20, 20)))
                    .addContainerGap()));
    jPanel1Layout
        .setVerticalGroup(jPanel1Layout
            .createParallelGroup(
                javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(
                jPanel1Layout
                    .createSequentialGroup()
                    .addContainerGap()
                    .addGroup(
                        jPanel1Layout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(
                                jTid,
                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel1))
                    .addGap(18, 18, 18)
                    .addGroup(
                        jPanel1Layout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel2)
                            .addComponent(
                                jTprenom,
                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(
                        jPanel1Layout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(
                                jTnom,
                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                    .addGap(18, 18, 18)
                    .addGroup(
                        jPanel1Layout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(
                                jTadresse,
                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel4))
                    .addGap(21, 21, 21)
                    .addGroup(
                        jPanel1Layout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(
                                jDateChooser2,
                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel5))
                    .addGroup(
                        jPanel1Layout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(
                                jPanel1Layout
                                    .createSequentialGroup()
                                    .addGap(28, 28, 28)
                                    .addComponent(jLabel6))
                            .addGroup(
                                javax.swing.GroupLayout.Alignment.TRAILING,
                                jPanel1Layout
                                    .createSequentialGroup()
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(
                                        jTlieu,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(
                        jPanel1Layout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(
                                jComboBox1,
                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addGroup(
                        jPanel1Layout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(
                                jTprofession,
                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8))
                    .addContainerGap()));
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(layout
        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(
            layout.createSequentialGroup()
                .addGap(45, 45, 45)
                .addComponent(jButton1)
                .addGap(70, 70, 70)
                .addComponent(jButton2,
                    javax.swing.GroupLayout.PREFERRED_SIZE,
                    117,
                    javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(
                    javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                    64, Short.MAX_VALUE)
                .addComponent(jButton3,
                    javax.swing.GroupLayout.PREFERRED_SIZE,
                    110,
                    javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(20, 20, 20))
        .addGroup(
            javax.swing.GroupLayout.Alignment.TRAILING,
            layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(
                            jPanel1,
                            javax.swing.GroupLayout.DEFAULT_SIZE,
                            javax.swing.GroupLayout.DEFAULT_SIZE,
                            Short.MAX_VALUE)
                        .addGroup(
                            layout.createSequentialGroup()
                                .addGap(0, 0, Short.MAX_VALUE)
                                .addComponent(
                                    jLabel9,
                                    javax.swing.GroupLayout.PREFERRED_SIZE,
                                    261,
                                    javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(112, 112, 112)));
    layout.setVerticalGroup(layout
        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(
            layout.createSequentialGroup()
                .addGap(9, 9, 9)
                .addComponent(jLabel9)
                .addGap(18, 18, 18)
                .addComponent(jPanel1,
                    javax.swing.GroupLayout.DEFAULT_SIZE,
                    javax.swing.GroupLayout.DEFAULT_SIZE,
                    Short.MAX_VALUE)
                .addGap(47, 47, 47)
                .addGroup(
                    layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jButton2)
                        .addComponent(jButton3)
                        .addComponent(jButton1))
                .addGap(37, 37, 37)));
    pack();
  }// </editor-fold>

  private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    System.exit(0);
  }

  private void jTlieuActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
  }

  private void jTadresseActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
  }

  private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
    // TODO add your handling code here:
    String Numero = jTid.getText();
    String prenom = jTprenom.getText();
    String nom = jTnom.getText();
    String adresse = jTadresse.getText();
    SimpleDateFormat sp = new SimpleDateFormat("dd/MM/yyyy");
    String date = sp.format(jDateChooser1.getDate());
    String lieu = jTlieu.getText();
    String sexe = (String) jComboBox1.getSelectedItem();
    String profession = jTprofession.getText();
    String requete = "INSERT INTO membres (Prenom, Nom, Adresse,Date, Lieu, Sexe, Profession)VALUES ('"
        + prenom + "','" + nom + "','" + adresse + "','" + date + "', '" + lieu + "', '" + sexe + "','" + profession + "') ";
    try {
      st = conn.createStatement();
      st.executeUpdate(requete);
      JOptionPane.showMessageDialog(null, "requete execut?e");
    } catch (SQLException ex) {
      Logger.getLogger(Formulaire.class.getName()).log(Level.SEVERE, null, ex);
    }
  }

  /**
   * @param args
   *      the command line arguments
   */
  public static void main(String args[]) {
    /* Set the Nimbus look and feel */
    // <editor-fold defaultstate="collapsed"
    // desc=" Look and feel setting code (optional) ">
    /*
     * If Nimbus (introduced in Java SE 6) is not available, stay with the
     * default look and feel.
     * 
     * For details see
     * [http://download.oracle.com/javase/tutorial/uiswing/lookandfeel
     * /plaf.html]
     */
    try {
      for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
        if ("Nimbus".equals(info.getName())) {
          javax.swing.UIManager.setLookAndFeel(info.getClassName());
          break;
        }
      }
    } catch (ClassNotFoundException ex) {
      java.util.logging.Logger.getLogger(Formulaire.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (InstantiationException ex) {
      java.util.logging.Logger.getLogger(Formulaire.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (IllegalAccessException ex) {
      java.util.logging.Logger.getLogger(Formulaire.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    } catch (javax.swing.UnsupportedLookAndFeelException ex) {
      java.util.logging.Logger.getLogger(Formulaire.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
    }
    // </editor-fold>
    /* Create and display the form */
    java.awt.EventQueue.invokeLater(new Runnable() {
      public void run() {
        new Formulaire().setVisible(true);
      }
    });
  }

  // Variables declaration - do not modify
  private javax.swing.JButton jButton1;
  private javax.swing.JButton jButton2;
  private javax.swing.JButton jButton3;
  private javax.swing.JComboBox jComboBox1;
  private com.toedter.calendar.JDateChooser jDateChooser1;
  private com.toedter.calendar.JDateChooser jDateChooser2;
  private javax.swing.JLabel jLabel1;
  private javax.swing.JLabel jLabel2;
  private javax.swing.JLabel jLabel3;
  private javax.swing.JLabel jLabel4;
  private javax.swing.JLabel jLabel5;
  private javax.swing.JLabel jLabel6;
  private javax.swing.JLabel jLabel7;
  private javax.swing.JLabel jLabel8;
  private javax.swing.JLabel jLabel9;
  private javax.swing.JPanel jPanel1;
  private javax.swing.JTextField jTadresse;
  private javax.swing.JTextField jTid;
  private javax.swing.JTextField jTlieu;
  private javax.swing.JTextField jTnom;
  private javax.swing.JTextField jTprenom;
  private javax.swing.JTextField jTprofession;
  // End of variables declaration
}